how to check the internet connection?

5

I have the following code:

 ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        String estado=networkInfo.getState().toString();
        Log.i("conexion",estado);
        if (networkInfo != null && networkInfo.isConnected()) {
            // Operaciones http
            new     LeerDeInternet().execute("http://www.floatrates.com/daily/ars.json");

        } else {
            Toast.makeText(this, "Compruebe su conexión a internet",Toast.LENGTH_SHORT).show();
        }

The problem is that although I do not have wifi and data network signal does not work (!) it returns the CONNECTED status and therefore the process is executed and waiting for data. The only way to tell me to check the connection is to disconnect the mobile network.

    
asked by Los Milton 01.01.2017 в 14:32
source

2 answers

8

The code you have is used to check if you are connected to a network, but it does not tell you if that network has Internet access or not. For example, if you are in a private network without Internet access, or in a hotstpot where you can not access the Internet until a code is given, the code will say that you are connected even if you do not have Internet.

There is a similar question on the English site . According to some of your answers, after checking that the connection is correct, you should make a request or ping a server that will surely be active (eg Google or Facebook) to make sure that in addition to having such connection has access to the Internet.

This can be done as follows (code adapted from the Razzak response ):

public boolean conectadoAInternet() throws InterruptedException, IOException
{
    String comando = "ping -c 1 google.com";
    return (Runtime.getRuntime().exec (comando).waitFor() == 0);
}

One problem would be that if Google is down, even if the device has access to the Internet, it will say that you do not have it. One option would be to add some type of fallback , for example if Google does not return the ping , check Facebook or Twitter or Amazon ... if none of them responds, you can be sure that there is no Internet (or that the world is running out: P)

    
answered by 01.01.2017 в 14:55
6

One thing is to obtain if your network is enabled and the other is to obtain if in reality there is connectivity depending on the providers, the most used providers are mobile type ( TYPE_MOBILE ) and type WiFi ( TYPE_WIFI ).

What you should review are two things:

If there is connectivity with isAvailable () and if there is connectivity and it is possible to establish a connection with < strong> isConnected ()

public static boolean isOnline(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

The way you would use the method would be:

 if (isOnline(getApplicationContext())) {
            // Operaciones http
            new     LeerDeInternet().execute("http://www.floatrates.com/daily/ars.json");

        } else {
            Toast.makeText(this, "Compruebe su conexión a internet",Toast.LENGTH_SHORT).show();
        }

Do not forget the permissions:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <uses-permission android:name="android.permission.INTERNET" />

I do not consider it appropriate to use the InetAddress class or to ping to check if there is connectivity and it is possible to make the connection, for this purpose are the methods contained in the SDK of the class NetWorkInfo which are:

  

isAvailable () : Indicates if network connectivity is possible.

     

isConnected () : Indicates whether network connectivity exists and is   possible to establish connections and send data.

and even

  

isConnectedOrConnecting () : Indicates whether network connectivity   It exists or is in the process of being established.

    
answered by 02.01.2017 в 19:13