我正在向 Facebook 图形 API 发出 HTTP-get 请求。
在大约 1/5 次中,我的代码永远不会到达Log.i("debug", "resp");
. 没有抛出异常。不应该吗?还是默认情况下它只是一个很长的超时?
如果我添加自定义超时(见下文),我会抛出异常。但即使我的代码包含在 try+catch 语句中,我的应用程序也会崩溃(就像任何未处理的异常一样),而不是让我处理onPostExecute()
. 为什么我不使用该方法?
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
}
}