Android ThreadSafeClientConnManager 超时?

发布于 2024-09-13 18:24:09 字数 1096 浏览 4 评论 0原文

我正在使用 ThreadSafeClientConnManager 在 Android 上的后台线程中执行同时请求,设置为:

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

ClientConnectionManager connman = new ThreadSafeClientConnManager(params, registry);
client = new DefaultHttpClient(connman, params);

并执行请求(请注意,我正在使用 outh-signpost):

HttpGet request = new HttpGet("https://" + API_HOST + "/" + API_VERSION + path);
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(key, secret);
consumer.sign(request);
HttpResponse response = client.execute(request);

问题是,过了一会儿我开始得到

java.net.SocketException: The operation timed out

我需要吗在请求后执行一些操作来显式释放连接?

I'm using a ThreadSafeClientConnManager to perform simultaneous requests in background threads on Android, set up with:

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

ClientConnectionManager connman = new ThreadSafeClientConnManager(params, registry);
client = new DefaultHttpClient(connman, params);

And executing requests with (note that I'm using outh-signpost):

HttpGet request = new HttpGet("https://" + API_HOST + "/" + API_VERSION + path);
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(key, secret);
consumer.sign(request);
HttpResponse response = client.execute(request);

The problem is that after a while I start getting

java.net.SocketException: The operation timed out

Do I need to do something to explicitly release the connection after the request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2024-09-20 18:24:09

根据 apache httpclient 的使用指南,您需要确保消耗任何池资源上的所有内容,以保证它返回到池以便稍后可供其他线程使用 -

http://hc.apache.org/httpcomponents-core-4.0.1/tutorial/html/fundamentals .html#d0e244

如果底层库抛出异常,最好中止您尝试运行的 HttpMethod,在这种情况下,连接将被终止。

From the usage guide of apache httpclient, you need to make sure to consume all content on any pooled resource to guarantee it returns to the pool to be available for other threads later on -

http://hc.apache.org/httpcomponents-core-4.0.1/tutorial/html/fundamentals.html#d0e244

In case there's an exception thrown by the underlying library, it is best to abort the HttpMethod that you were trying to run, in which case the connection will be terminated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文