ErrnoException: isConnected failed: EHOSTUNREACH (No route to host) when changing the wifi network using ICS

回眸只為那壹抹淺笑 提交于 2019-12-03 10:40:37

Maybe it's taking a while for the switch over to take place for some reason? You can check to see if there is an active network connection before making the request:

ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo                 = connectivityManager.getActiveNetworkInfo();

if (networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable())
{
    // DO WHAT YOU NEED TO DO ON THE NETWORK
}
else
{
    // PROMPT USER THAT NETWORK IS DISCONNECTED

        Toast.makeText(this, "There is no active network connection!", 5000).show();
}
Manish Lomte

While switching and establishing with new network device may take some time. The below code may help you.

ConnectivityManager connMngr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
    return connMngr.getActiveNetworkInfo().isConnectedOrConnecting();
}
catch (NullPointerException npe) {
    return false;
}

Looks like you have problem with TCP protocol. It maybe in case weak WiFi or 3G signal. Use try/catch for it.

Matthias Ronge

failed: EHOSTUNREACH (No route to host)

So the routing has not been set up correctly.

I found that for some devices you have to set the WifiConfiguration’s ipAssignment field to WifiConfiguration.IpAssignment.DHCP manually for that a DHCP request is performed after changing the wifi programatically. This can only be done via reflection, see this answer how to code it. Catch all exceptions, because on other devices the field does not even exist.

I had the same problem this is how I solved it.

  1. Disable your firewalls
  2. go to your server folder (in my case I was using Node.js) the WWW file and listen on the provided port, on all network interfaces.

    server.listen(port, '10.8.75.204');

    server.on('error', onError);
    server.on('listening', onListening);
    
  3. Make sure the IP address is your local network IP, on widows this can be gotten by typing ipconfig on command prompt.

  4. restart the server and try connecting again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!