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

Exception or error:

When using my app on ics, after I change my wifi network from networkA to networkB all the requests for images start coming back with an exception.

failed with exception

> org.apache.http.conn.HttpHostConnectException: Connection to
> https://m1.testapp.com refused    at
> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
>   at
> org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
>   at
> org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
>   at
> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
>   at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
>   at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
>   at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
>   at
> com.testApp.android.ws.PooledRequestProcessor$Runner.run(PooledRequestProcessor.java:298)
>   at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
>   at java.lang.Thread.run(Thread.java:856)  Caused by:
> java.net.ConnectException: failed to connect to /109.233.153.38 (port
> 443) after 20000ms: isConnected failed: EHOSTUNREACH (No route to
> host)     at
> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:181)
>   ... 10 more  Caused by: java.net.SocketException: failed to connect
> to /109.233.153.38 (port 443) after 20000ms: isConnected failed:
> EHOSTUNREACH (No route to host)   at
> libcore.io.IoBridge.isConnected(IoBridge.java:220)    at
> libcore.io.IoBridge.connectErrno(IoBridge.java:152)   at
> libcore.io.IoBridge.connect(IoBridge.java:112)    at
> java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)    at
> java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)    at
> java.net.Socket.connect(Socket.java:842)      at
> org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
>   at
> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
>   ... 10 more  Caused by: libcore.io.ErrnoException: isConnected
> failed: EHOSTUNREACH (No route to host)   at
> libcore.io.IoBridge.isConnected(IoBridge.java:201)

The strangest thing is that even uninstalling the app and reinstalling it wont fix it, just turning the phone off and on.

I saw a similar problem here http://groups.google.com/group/newsrob/browse_thread/thread/ea2f26d9d1753b79/5800e268eeab399c#5800e268eeab399c . The problem is not happening on phones with 4.0.3, just on the ones with 4.0.1, and the update doesnt seem to be available in Europe yet.

How to solve:

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();
}

###

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.

###

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.

Leave a Reply

Your email address will not be published. Required fields are marked *