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?
P.S 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();
}
}