Android是否支持同时多个HTTP请求?

发布于 2024-12-05 18:09:47 字数 145 浏览 8 评论 0原文

在我的应用程序中,我想同时处理多个文件的下载。 为此,我启动了多项服务,每个服务对应一个请求。 我不确定 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 技术交流群。

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

发布评论

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

评论(3

长途伴 2024-12-12 18:09:47

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

宣告ˉ结束 2024-12-12 18:09:47

当配备池连接管理器时,例如 ThreadSafeClientConnManagerHttpClient 可用于使用多个执行线程同时执行多个请求。

以下是有关如何使用它的完整示例:2.9。多线程请求执行

更新:花了一段时间,但 ThreadSafeClientConnManager 现已弃用(请参阅下面的摘录自 Apache Http 客户端删除):

Android 6.0 版本删除了对 Apache HTTP 客户端的支持。如果您的应用程序正在使用此客户端并且面向 Android 2.3(API 级别 9)或更高版本,请改用 HttpURLConnection 类。该 API 更加高效,因为它通过透明压缩和响应缓存减少了网络使用,并最大限度地降低了功耗。

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):

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption.

泼猴你往哪里跑 2024-12-12 18:09:47

进行一些测试以确定有多少并发 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.

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