gpt4 book ai didi

Android HttpPost 超时异常不工作

转载 作者:行者123 更新时间:2023-11-30 04:07:07 26 4
gpt4 key购买 nike

我正在尝试将我的应用程序设置为在 HttpPost 上 3 秒后抛出超时异常。我正在从 ASyncTask 执行此请求。出于某种原因,即使我给它一个不存在的域,它也会挂起大约一两分钟,然后抛出最后一个异常。为什么我的超时异常不起作用?

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;

}

最佳答案

不要在那里调用 finish。

当出现带有特定错误消息的异常时,为什么不返回 null。

在postexecute方法中,当结果为null时,做任何需要做的事情。

关于Android HttpPost 超时异常不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11419278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com