Java: Internet Connectivity Check Via Ping To Google Not Working
I am using the following code to check internet connectivity: try { HttpURLConnection httpConnection = (HttpURLConnection) (new URL('http://clients3.google.
Solution 1:
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
Solution 2:
If you trust your Android device then you could just use this method:
publicbooleanisOnline() {
ConnectivityManagercm=
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetInfo= cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
returntrue;
}
returnfalse;
}
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
Solution 3:
publicBoolean 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();
}
returnfalse;
}
Post a Comment for "Java: Internet Connectivity Check Via Ping To Google Not Working"