Android是否支持同时多个HTTP请求?
在我的应用程序中,我想同时处理多个文件的下载。 为此,我启动了多项服务,每个服务对应一个请求。 我不确定 Android 是否支持并行并发 http 请求?
那么,每个请求有一个 HTTPClient 是好习惯还是坏习惯呢?
非常感谢您的帮助!
In my app I'd like to handle downloading of several files at the same time.
To do so I'm starting several services, one for each request.
As I'm not sure, does Android support simultaneous http requests in parallel?
In that case, is it good or bad habit to have one HTTPClient per request?
Many thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HttpClient 不是异步的,并且本身不支持并行连接。您可以有多个线程,每个线程使用单独的 HttpClient 实例执行下载。
您可能还想查看 ExecutorService:http://developer.android。 com/reference/java/util/concurrent/ExecutorService.html
HttpClient is not asynchronous and does not support parallel connections per se. You could have multiple threads each performing download with separate HttpClient instances.
You might also want to look at ExecutorService: http://developer.android.com/reference/java/util/concurrent/ExecutorService.html
当配备池连接管理器时,例如
ThreadSafeClientConnManager
、HttpClient
可用于使用多个执行线程同时执行多个请求。以下是有关如何使用它的完整示例:2.9。多线程请求执行。
更新:花了一段时间,但
ThreadSafeClientConnManager
现已弃用(请参阅下面的摘录自 Apache Http 客户端删除):When equipped with a pooling connection manager such as
ThreadSafeClientConnManager
,HttpClient
can be used to execute multiple requests simultaneously using multiple threads of execution.Here is a full example on how to use it: 2.9. Multithreaded request execution.
Update: It took a while, but the
ThreadSafeClientConnManager
is now deprecated (see excerpt below from Apache Http Client Removal):进行一些测试以确定有多少并发 HTTPRequest 可以正常工作。
我建议启动一项服务并拥有多个线程,而不是多个服务。
Do some testing to determine how many concurrent HTTPRequests work well.
I recommend starting one service and having many Threads rather than multiple services.