Check internet connectivity in Android

When programming an Android App you sometimes want to check, if you are connected to the Internet.
The first thing that comes to mind is using the ConenctivityManager. Yet this solution has the problem that it only tells you that you are connected to .. something. With a WiFi connection this can mean, that you are connected to an access point. But it does not tell you that the access point is connected to something else!


Common problems you can run into at this point are:

  1. The internet connection of the router, the access point is connected to, is down.
  2. You are in a guest network (hotel, airport, …) and need to authenticate via TextMessage, web login before you get access to the web.
  3. You are in a closed (copmany) network and won’t get to the web for security policy reasons.

In all cases, the network manager tells you “you have WiFi”.

Okay, first thing that comes to mind next might be: “Access a random website (or your own one) and check for a 200 return code.”

This won’t work. Think about #2 above. You hook up a hotel network, open a browser and it shows you the login screen. If the return code is 200, you might assume that you can access the web.

The safest and most reliable way to check the connectivity is the following solution that is used by android as well:
Check http://clients3.google.com/generate_204 for status code 204 (no content) and content length 0. Of course you can also set up an own generate_204 url on your own host. This has the advantage that you check the connectivity to really your site (if your website could be subject to be blacklisted by company firewalls). But think about a reasonable error message in this case “You don’t have a connection” might be very misleading if the user can go to Google but not to your website. Better choose something like “the access to our website is unavailable / blocked”.
Also ensure that the URL is really really stable. If someone decides to change the status code or the URL you will get false errors in the App!

Okay, enough theory: Here is the necessary code found on StackOverflow:

public static boolean hasInternetAccess(Context context) {
    if (isNetworkAvailable(context)) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) 
                (new URL("http://clients3.google.com/generate_204")
                .openConnection());
            urlc.setRequestProperty("User-Agent", "Android");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500); 
            urlc.connect();
            return (urlc.getResponseCode() == 204 &&
                        urlc.getContentLength() == 0);
        } catch (IOException e) {
            Log.e(TAG, "Error checking internet connection", e);
        }
    } else {
        Log.d(TAG, "No network available!");
    }
    return false;
}

Have fun!

One thought on “Check internet connectivity in Android”

  1. The sad part is that the corresponding code in Android/Chrome doesn’t even handle the errors it gets back and instead of saying “ah, my connection call didn’t work” it will go “ah, my proxy returned , but that’s not 204.”
    Really a good thing those people don’t write systems code “anywhere”.
    I wish you all the best and may you always code nicer than the people on certain projects.

Comments are closed.