【发布时间】:2011-03-10 00:54:31
【问题描述】:
在将 HTTP 请求从 Android 应用程序发送到外部服务器时,重复使用 DefaultHTTPClient 的优点和缺点是什么?我尝试在发出周期性HTTPGet 请求时重用DefaultHTTPClient,但我得到随机套接字超时(特别是在使用3G 时)。
我的代码如下:
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 对象)。
有什么建议吗?
【问题讨论】:
-
我开始看到相同的行为,从 Android 2.3.3 开始。我的应用程序以前运行良好,但现在升级到 2.3.3 后,有时会出现 java.net.SocketException: Connection timed out。我有 20 秒的套接字超时和连接超时。
标签: android