Android的HttpPost超时异常不工作
问题描述:
我想设置我的应用程序在HttpPost 3秒后引发超时异常。我正在从ASyncTask执行此请求。出于某种原因,即使我给它一个不存在的域,它也会挂起大约一两分钟,然后抛出最后一个异常。我的超时例外怎么不起作用?Android的HttpPost超时异常不工作
protected Void doInBackground(String... params) {
HttpPost httpPost = new HttpPost("http://hgfdhgfdhgfdhfgdhgfdhgfdhfgd.com");
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 3000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch(ConnectTimeoutException e){
Log.e("Timeout Exception: ", e.toString());
} catch(SocketTimeoutException ste){
Log.e("Timeout Exception: ", ste.toString());
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
答
不要在那里叫完。
为什么当出现带有特定错误消息的异常时不返回空值。
在postexecute方法中,当结果为null时,做任何需要做的事情。
我不是故意要保持完成的在那里......我在最后一分钟里扔了一下,看看是否会完成异常活动。我编辑了我的帖子。 – 2012-07-10 18:13:11
通过将我的任务移动到onPostExecute来实现它。谢谢! – 2012-07-10 18:38:40
@你以前的任务是什么? – Hades 2012-07-12 07:08:16