I’m making HTTP-get requests to the Facebook graph API.
In about 1/5 times my code never gets to Log.i("debug", "resp");. No exception thrown. Shouldn’t it? Or is it just a very long timeout by default?
If I add a custom timeout (see below), I get to throw an exception. But even though my code is wrapped in a try+catch statement, my app crashes (just like on any unhandled exceptions), instead of letting me handle the error in onPostExecute(). Why dont I end up in the method?
protected Map<String, Integer> doInBackground(Void... params) {
Map<String, Integer> result = new HashMap<String, Integer>();
try {
HttpGet get = new HttpGet("https://graph.facebook.com/....etc");
//final HttpParams httpParams = httpclient.getParams();
//HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
//HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpResponse response = httpclient.execute(get);
Log.i("debug", "resp");
HttpEntity resEntityGet = response.getEntity();
//do stuff with resEntityGet
return result;
} catch (Exception e) {
Toast.makeText(mainActivity, "Error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
return null;
}
}
protected void onPostExecute(Map<String, Integer> result) {
if(result != null){
//use the result data
} else {
//exception occured
}
}
You can show a
Toastfrom thedoInBackgroundmethod if you post it to the UI thread. There’s a few ways to do it, but here’s one way:You’ll need to make the
Exceptionvariablefinal