简体   繁体   中英

How to actively check internet connection or to check connection timeout in android?

In my app if there is no internet connection i get the following result on the onPostExecute() in my asynctask method

"Value Unable of type java.lang.String cannot be converted to JSONArray".

Therefore I need to check for constant internet connection. Also if there is a connection timeout I need to show a message to the user.

So, how can I solve this problem?

PS now i am checking if there is no internet connecting from the following code

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class InternerConnection {
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}

but if there is a bad network i'm getting the above quoted message.

EDITED

here is my onPostExecute and doInBackground methods

@Override
protected void onPostExecute(String result) {

try {
// dialog.dismiss();
Log.v("esty", "Restaurent output :" + result);

Restaurants rd = new Restaurants();
JSONObject json = new JSONObject(result);

JSONObject obj = json.getJSONObject("Restaurant");
String ID = obj.getString("Id");

String RestaurantBrandId = obj.getString("RestaurantBrandId");

String RestaurantBrand = obj.getString("RestaurantBrand");

/*.......................... Rest of the JSON parsing
..................................................*/
((RestaurantsDetailsGeneralActivity) activity).Bind(rd);

super.onPostExecute(result);
} catch (Exception ex) {

Log.v("error", "Restaurant detail :" + ex.getMessage());

}
}


@Override
protected String doInBackground(Void... params) {

try {



HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpParams p = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(p);

String url = "http://89b69f2297434a0eac543944c620faae.cloudapp.net/RestaurantsApi/Details";



List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("restaurantNumber",
AppGlobal.ResturantNumber));


HttpClient httpClient = new DefaultHttpClient();
String paramsString = URLEncodedUtils.format(nameValuePairs,
"UTF-8");
HttpGet httpget = new HttpGet(url + "?" + paramsString);

ResponseHandler<String> responseHandler = new BasicResponseHandler();

String responseBody = httpclient.execute(httpget, responseHandler);

return responseBody;
} catch(ConnectException e){             
  //not getting this exception. Do in background always returns a result.         
       return e.getMessage();
   }catch (SocketTimeoutException ste){
       Log.v("esty", ste.toString());

       return ste.toString();
   }catch (Exception ex) {

return ex.getMessage();
}

}

It seems like the best thing to do would be to find out why you are getting that particular error when a connection failure occurs, and fix your code so that you can handle the failure gracefully. It would probably be easier than attempting to listen for a timeout. Can you post some of your code in your onPostExecute method?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM