Issue
I am developing an application where i want to access whether there is internet connectivity or not. I can access the network state by using
private Boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
But this just returns whether the mobile is connected to internet or not. I want to know if there is internet availability after connecting. Like, may be the server is down or internet not available. Please let me know how to accomplish this!
Solution
Try like this,
URL url = new URL("Your URL");
URLConnection conexion = url.openConnection();
conexion.setConnectTimeout(10000); // Don't forget to put a time limit
conexion.connect();
After timeout (10 secs for above mentioned example), It will through time out exception. So You can use it to check whether Internet access is available or not.
Answered By - surender8388
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.