0

I am using the following code to check internet connectivity:

try {
                        HttpURLConnection httpConnection = (HttpURLConnection) (new URL("http://clients3.google.com/generate_204").openConnection());
                        httpConnection.setRequestProperty("User-Agent", "Test");
                        httpConnection.setRequestProperty("Connection", "close");
                        httpConnection.setConnectTimeout(15000); 
                        httpConnection.connect();
                        if (httpConnection.getResponseCode() == 204 && httpConnection.getContentLength() == 0){
                        //internet is avialable

                            return;
                        }else{
                             Log.e(TAG, "Internet connection error: " + httpConnection.getResponseCode()
                                     + ": " + httpConnection.getResponseMessage());
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Internet connection error: " + e);
                    }

And i am getting the following response : code: 204 message: No Content

but content length is greater than 0 and hence it fails. Could some one please help me understand what is going on?

Thanks, Sunny

Sunny
  • 7,444
  • 22
  • 63
  • 104
  • I run your example with Java 1.7_45 and it works as expected. ? – morpheus05 Oct 20 '14 at 20:01
  • on some phone the content length does not seem to be 0. is that acceptable? – Sunny Oct 20 '14 at 20:02
  • http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html says that: "The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields." Maybe this newline is counted as content by some implementations? – morpheus05 Oct 20 '14 at 20:04
  • so if i remove the check for content length to be 0. then would the code to check internet connectivity still work fine? – Sunny Oct 20 '14 at 20:05
  • I would say so. I mean its your decision to ignore or not to ignore the content of a 204. But in the end: If the server returns 204, you reached the server so obvs. you have internet (-: – morpheus05 Oct 20 '14 at 20:07

3 Answers3

1
public Boolean isOnline() {
    try {
        Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
        int returnVal = p1.waitFor();
        boolean reachable = (returnVal==0);
        return reachable;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}
sami rahimi
  • 13
  • 1
  • 5
  • 2
    Congratulations on your first answer. Quality would be improved if you explained what you are doing and not just give a code dump. – John3136 Aug 19 '17 at 23:35
0

according to the title you want to ping google, but you are requesting an Site via HTTP, and these two things are not the same. So if you want to ping in Android visit this post: How to Ping External IP from Java Android

Community
  • 1
  • 1
jollyjoyce1995
  • 322
  • 4
  • 15
  • i actually just want to check whether there is internet connection in the phone. will the above code not work? – Sunny Oct 20 '14 at 20:12
  • Well if you get an respone from the server then you have Internet, however, if it throws an error or an Exception then you simply dont have Internet. So you can use your code to check if you have Internet :) – jollyjoyce1995 Oct 20 '14 at 20:21
  • which one is one lightweight and recommended according to you for an android app. – Sunny Oct 20 '14 at 20:33
0

If you trust your Android device then you could just use this method:

public boolean isOnline() {
ConnectivityManager cm =
    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    return true;
}
return false;

}

But if you want to do it on your own then you should use the ping method because it requires less resources than an HTTP Request

The method above basically askes the android system if you have internet and if you ping google then you just do it on your own

jollyjoyce1995
  • 322
  • 4
  • 15
  • the problem with this method is that it will detect if you are connected to Wifi. But it will fail to detect if the Wifi has an internet connection or not ...hence this wont work – Sunny Oct 20 '14 at 20:55