0

I uploaded my app recently to Google Playstore. I used Error Reporter to track the crashes. App is working fine but very frequently I get HttpHostConnectException. Before making every web-call, I checked for Internet Connection. Are there any other reasons for the cause of this exception? How can it be avoided?

P.S. I never get this exception while testing/debugging my app.

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109

2 Answers2

0

HttpHostConnectException is thrown when connection cannot be established to a remote host on a specific port.

Before making every web-call, I checked for Internet Connection.

Checking internet connection is not a full-proof way to decide that the host is reachable. In many instances like using wifi, the device is connected to your router while the router is not connected to the internet. Checking internet connection using classes like ConnectivityManager in such cases returns true but the actual connection is false.

The solution is to check if your host is actually reachable using any http methods.

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name

        if (ipAddr.equals("")) {
            return false;
        } else {
            return true;
        }

    } catch (Exception e) {
        return false;
    }

}

The above code is taken from this SO post.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
0

I used AsyncHttpClient to handle all my webcalls. It handles my case perfectly. It directly takes to onFailure() on getting HttphostConnectException.

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109