套接字超时使得使用DefaultHTTPClient HTTPGET请求时 [英] Socket timeout when making HTTPGet requests using DefaultHTTPClient
问题描述
有哪些优点和缺点背后重用 DefaultHTTPClient
从Android应用程序发送HTTP请求时到外部服务器?我试图用重复使用 DefaultHTTPClient
制作时,周期性 HTTPGET
的要求,但我得到随机插座超时(特别是使用3G的时候)
What are the pros and cons behind reusing DefaultHTTPClient
when sending HTTP requests from an Android app to an external server? I tried using reusing a DefaultHTTPClient
when making periodic HTTPGet
requests, but I get random socket timeouts (specially when using 3G).
我的code看起来如下:
My code looks like follows:
public class MyHTTPSender {
private DefaultHTTPClient mClient;
public MyHTTPSender() {
mClient = new DefaultHTTPClient();
}
public void send(String httpAddress) {
HttpGet get = new HttpGet(this.surrogateURL);
HttpResponse response = null;
try {
response = httpClient.execute(get);
// ... consume entity if OK
} catch (Exception e) {
} finally {
if (response != null) {
// do some sanity checks to ensure Entity is there!
response.getEntity().consumeContent();
}
}
}
}
我看不出什么问题,我在做什么。我有一个单独的处理程序,使HTTPPost请求,并且工作得很好(使用不同的DefaultHTTPClient对象)。
I can't see anything wrong with what I am doing. I have a separate handler that make HTTPPost requests, and that works perfectly well (uses a different DefaultHTTPClient object).
有什么建议?
推荐答案
什么API级别是你吗?
What API level are you on?
如果你是在8级以上,你可能会考虑尝试 AndroidHttpClient < /一>,它可以指定一个较好的插座超时。
If you're on 8 or above you might consider trying AndroidHttpClient, which may have a better socket timeouts specified.
否则,你可能会检查你如何创建 DefaultHttpClient
并尝试指定更长的超时。
Otherwise, you might examine how you're creating the DefaultHttpClient
and try specifying longer timeouts.
这篇关于套接字超时使得使用DefaultHTTPClient HTTPGET请求时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!