gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 12:57:10 24 4
gpt4 key购买 nike

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

我用谷歌搜索了一个解决方案,我看到 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");

More info

最佳答案

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

因为HttpClient默认不是线程安全的。您必须为此连接一个 ThreadSafeClientConnManager。这包含在 HttpClient 中。文档。

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

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

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

大多数程序员也可以使用 HttpClient 做到这一点。

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

你不能。

关于java - 如何在 Android 上使用最新的 HttpComponents 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5154399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com