【发布时间】:2012-03-01 20:18:40
【问题描述】:
我在 Android 中请求 POST/GET 时遇到问题。 我正在尝试处理错误:
引起:java.net.SocketTimeoutException: Read timed out
为了防止我的应用程序崩溃,我添加了 40 秒的超时。这可行,但有时 40 秒不足以避免错误。
我尝试添加“try and catch”,但似乎这里没有发生错误:
try {
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
localContext.setAttribute(ClientContext.COOKIE_STORE,
Backend2.cookieStore);
response = HttpManager.execute(request, localContext);
if (response.getEntity() != null) {
final String r = EntityUtils.toString(response.getEntity());
return r;
} else {
return null;
}
} catch (SocketTimeoutException e) {
e.printStackTrace();
return null;
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
我正在寻找解决方案,但是当我在 stackoverflow 和 google 上阅读时,我只看到诸如“增加您的超时时间,添加尝试等”之类的帖子。
我做错了什么?
【问题讨论】: