如何在 Android 上使用最新的 HttpComponents 版本?
我使用的是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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为HttpClient默认情况下不是线程安全的。您必须为此连接一个 ThreadSafeClientConnManager 。 HttpClient 文档。
https://android.googlesource.com/platform/external/apache-http
请注意,自“2008 年 7 月 1 日”以来有相当多的提交。
大多数程序员也可以使用 HttpClient 来完成此操作。
你不能。
Because HttpClient is not thread-safe by default. You have to hook up a
ThreadSafeClientConnManager
for that. This is covered in the HttpClient documentation.https://android.googlesource.com/platform/external/apache-http
Please notice that there are quite a few commits since "01 July 2008".
Most programmers can do this with HttpClient as well.
You can't.