Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'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!

like image 514
Romain Piel Avatar asked Sep 21 '11 14:09

Romain Piel


2 Answers

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

like image 58
Peter Knego Avatar answered Nov 04 '22 11:11

Peter Knego


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.

like image 5
Håvard Geithus Avatar answered Nov 04 '22 12:11

Håvard Geithus