如何在 Android 上使用最新的 HttpComponents 版本?

发布于 2024-10-20 03:47:28 字数 1423 浏览 2 评论 0原文

我使用的是Android自带的HttpComponents库。我编写了 2 个类来使用 GET 和 POST 发送参数,并从服务器接收 InputStream 或字符串。一切工作正常:线程上的每次下载、开始、暂停、恢复...但是今天我同时开始了两个下载,但没有一个完成。

我用谷歌搜索了一个解决方案,我看到Android附带了HttpCore 4.0-beta2。是的,它很旧了。它于 2008 年 7 月 1 日发布,此后 Android 就没有更新过它...

我重写了我的类,但现在使用 HttpURLConnection,我可以同时下载两个文件。

好的,如果您正在考虑在 Android 上使用 HttpComponents,那么这对您来说是一个警告。现在我的问题。我想将新版本 4.1 导入到我的项目中。我已将 jar 添加到构建路径中,但如果新(4.1)和旧(beta2、4.0)包相同,现在如何使用这些 jar?

谢谢。

已解决
您必须使用 ThreadSafeClientConnManager 创建 DefaultHttpCLinet:

HttpParams parameters = new BasicHttpParams ();
HttpProtocolParams.setVersion (parameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset (parameters, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue (parameters, false);
ConnManagerParams.setMaxTotalConnections (parameters, MAX_CONNECTIONS);

SchemeRegistry schReg = new SchemeRegistry ();
schReg.register (new Scheme ("http", PlainSocketFactory.getSocketFactory (), 80));

client = new DefaultHttpClient (new ThreadSafeClientConnManager (parameters, schReg), parameters);

更新:您还可以使用 AndroidHttpClient。使用此类,您不需要使用 ThreadSafeClientConnManager

AndroidHttpClient client = AndroidHttpClient.newInstance ("Android");

更多信息

I was using the HttpComponents library that Android comes with. I wrote 2 classes to send params with GET and POST and to recieve an InputStream or a String from the server. All was working fine: every download on a thread, start, pause, resume... But today I've started two downloads simultaneously and none of them have finished.

I've googled a solution and I've seen that Android comes with HttpCore 4.0-beta2. Yes, it's very old. It was released on 01 July 2008 and Android hasn't updated it since then...

I've rewritten my classes but now using HttpURLConnection and I can download two files at the same time.

OK, that was a warning for you if you are considering using HttpComponents on Android. Now my question. I want to import the new version 4.1 to my project. I've added the jars to the built path, but now how can I use these jars if the new (4.1) and the old (beta2, 4.0) packages are the same?

Thanks.

SOLVED
You have to create the DefaultHttpCLinet with a ThreadSafeClientConnManager:

HttpParams parameters = new BasicHttpParams ();
HttpProtocolParams.setVersion (parameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset (parameters, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue (parameters, false);
ConnManagerParams.setMaxTotalConnections (parameters, MAX_CONNECTIONS);

SchemeRegistry schReg = new SchemeRegistry ();
schReg.register (new Scheme ("http", PlainSocketFactory.getSocketFactory (), 80));

client = new DefaultHttpClient (new ThreadSafeClientConnManager (parameters, schReg), parameters);

UPDATE: You can also use AndroidHttpClient. With this class you don't need to use ThreadSafeClientConnManager

AndroidHttpClient client = AndroidHttpClient.newInstance ("Android");

More info

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

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

发布评论

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

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-10-27 03:47:28

但是今天我同时开始了两个下载,但都没有完成。

因为HttpClient默认情况下不是线程安全的。您必须为此连接一个 ThreadSafeClientConnManager 。 HttpClient 文档。

我在 google 上搜索了一个解决方案,发现 Android 附带了 HttpCore 4.0-beta2。是的,它很旧了。它于 2008 年 7 月 1 日发布,从那时起 Android 就没有更新过它......而且它有 BUGGED

https://android.googlesource.com/platform/external/apache-http

请注意,自“2008 年 7 月 1 日”以来有相当多的提交。

我重写了我的类,但现在使用 HttpURLConnection,我可以同时下载两个文件。

大多数程序员也可以使用 HttpClient 来完成此操作。

如果新(4.1)和旧(beta2、4.0)包相同,我该如何使用这些 jar?

你不能。

But today I've started two downloads simultaneously none of them finished.

Because HttpClient is not thread-safe by default. You have to hook up a ThreadSafeClientConnManager for that. This is covered in the HttpClient documentation.

I've googled a solution and I've seen that Android comes with HttpCore 4.0-beta2. Yes, it's very old. It was released on 01 July 2008 and Android hasn't updated it since then... and it's BUGGED

https://android.googlesource.com/platform/external/apache-http

Please notice that there are quite a few commits since "01 July 2008".

I've rewritten my classes but now using HttpURLConnection and I can download two files at the same time.

Most programmers can do this with HttpClient as well.

how can I use these jars if the new (4.1) and the old (beta2, 4.0) packages are the same?

You can't.

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