1

Here's the Asynctask method:

public class Read extends AsyncTask<String, Integer, String> {
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            sUrl = sUrl.trim();
            json = lastTweet(sUrl);
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub 
    }

}

And related lastTweet method:

public JSONObject lastTweet(String username)
        throws ClientProtocolException, IOException, JSONException {
    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if (status == 200) {
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);
        return last;
    }
}

All of this code is working fine. No issues as of now. However, there's minor tinkering I want to do. As and when the connectivity is lost during HTTP transmission, a RemoteException is thrown and app crashes.

I tried to handle exception within Async method but not able to do so.

Any way to handle such an exception?

Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
Paritosh
  • 367
  • 5
  • 20
  • 1
    How did you try to handle it? You can always catch an exception, AsyncTasks don't do anything differently in that respect. – Gabe Sechan Jul 14 '13 at 19:33
  • Tried 2 things: 1. try catch block in lastTweet method for RemoteException 2. try catch for json = lastTweet(sUrl); statement. Neither of them worked. I know I am missing out something. – Paritosh Jul 14 '13 at 19:34

1 Answers1

1

you can check network connectivity before executing your Read AsyncTask

1. check Connection

if(ifConnectionIsAvailable)
    new Read().execute();

2. set Connection Time Out

HttpGet get = new HttpGet(url.toString());
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;// in milliseconds 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse r = httpClient.execute(get);
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
  • I am checking network connectivity. However, the problem arises when connectivity is lost during transmission (not before not after but during connection with server). That's when app crashes. – Paritosh Jul 14 '13 at 20:00
  • ok you want to check change in network state during transmission, if its no more connected to network than app should handle that during transmission instead of crash, am i right ? – Tarsem Singh Jul 14 '13 at 20:12
  • Right. As of now, all it does is gives up totally. – Paritosh Jul 14 '13 at 20:13
  • Okay. Found something. Not sure how to implement it but looks like a way out. [Monitor for Changes in Connectivity](http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html) – Paritosh Jul 14 '13 at 20:16
  • use connection time out ! like if code will check connection to respond for 30 seconds if after 30 seconds past, handle that ! – Tarsem Singh Jul 14 '13 at 20:19
  • Any code/link for reference. I can give it a shot and see if it works. But the way I see it, I need to either monitor connectivity on regular basis when Asynctask method is being executed or handle RemoteException. – Paritosh Jul 14 '13 at 20:21
  • Here's another solution - http://stackoverflow.com/questions/3307237/how-can-i-monitor-the-network-connection-status-in-android Again, how should I connect it with async method? – Paritosh Jul 14 '13 at 20:28
  • but i think timeout is best and easy which will not raise any extra load in your app, and also while executing http than how will you use that ? – Tarsem Singh Jul 14 '13 at 20:29
  • Here's a possible scenario I am trying to handle. The device has connectivity, http transmission starts, for some reason the device looses connectivity. In this case the app crashes. However, if I were to implement timeout, then the chances of similar scenario happening still remain. To explain, let's assume, the connectivity remain for 30 seconds (or so), http transmission starts, then again connectivity is lost and again app crashes. – Paritosh Jul 14 '13 at 20:34