user971741
user971741

Reputation:

How to check for internet connectivity before making a request

I have this usual code to make HTTP requests to the server.

      HttpClient httpclient = new DefaultHttpClient();
      ...
        HttpGet httpGet = new HttpGet(finalUri.toURI());
        httpGet.addHeader("Accept", "text/xml");

        response = httpclient.execute(httpGet);

This works fine when there is internet connectivity. But when there is no internet connectivity the code just wait for long time at httpclient.execute(httpGet); until it times out and throwing exception and crashing the application.

01-10 15:17:48.612  20692-20806/com.somepackage.somepackage  E/EXCEPTION﹕ Unable to resolve host "myapi.myhost.com": No address associated with hostname

For the exception I can catch UnknownHostException e and handle it. But still is there a way to know it prehand that internet connectivity is missing?

Upvotes: 0

Views: 1758

Answers (5)

Anggrayudi H
Anggrayudi H

Reputation: 15155

Add a new method inside your current Activity or Fragment:

private boolean isNetworkAvailable(){
        boolean available = false;
        /** Getting the system's connectivity service */
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        /** Getting active network interface  to get the network's status */
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if(networkInfo !=null && networkInfo.isAvailable())
            available = true;

        /** Returning the status of the network */
        return available;
    }

And here is how to use it. You can use it inside onCreate() method:

if (isNetworkAvailable() == true){ // if network is available
   // do your thing here
   ...
}else{ // "else" means that the network is not available
   // do your thing here
   ...
}

Upvotes: 0

dReAmEr
dReAmEr

Reputation: 7196

I would suggest to check this like below.You can check this by standard java.net.URLConnection class simply.

 try{
     URLConnection urlConnection = finalUri.openConnection();
     urlConnection.connect();
 }
 catch(Exception ex){
    //No connection to finalUri
    // return your error code for no connection
 }

Upvotes: 0

Karthikeyan
Karthikeyan

Reputation: 1147

Another way of checking internet connection status with connection type:

public boolean getConnectionStatus(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
if (conn == NetworkUtil.TYPE_WIFI||conn == NetworkUtil.TYPE_MOBILE) {
  return true;
} 
else{
   return false
}

}

Upvotes: 0

peterho
peterho

Reputation: 74

This works for me.

public boolean IsInternetAvailable(Context context)
{

    ConnectivityManager connectivityManager 
            = (ConnectivityManager) (context.getSystemService(Context.CONNECTIVITY_SERVICE));
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Upvotes: 0

hata
hata

Reputation: 12478

You can use NetworkInfo for the purpose.

private boolean checkNetworkConnection() {
    NetworkInfo networkInfo = ((ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isConnected()) {
        return false;
    } else {
        return true;
    }
}

Upvotes: 1

Related Questions