Does Android support multiple HTTP requests at the same time?
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开发者_C百科'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!
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
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.
Do some testing to determine how many concurrent HTTPRequests work well.
I recommend starting one service and having many Threads rather than multiple services.
精彩评论