Вопрос

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!

Это было полезно?

Решение 3

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.

Другие советы

To check that it's a working Internet connection and not just you are connected to Internet. You can do that by trying to fetch a known address/resource from your site, like a 1x1 PNG image or 1-byte text file. Also it will answer you about your server status. :)

Checking internet connection normally implemented, but if you want know your server working running or no, first you need send ping or send stub request(Example: send request with any params) to your server. If have response yes then you can work perfectly with your server.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top