16

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!

Romain Piel
  • 11,017
  • 15
  • 71
  • 106
  • I don't think there is any other way. You can make just one httprequest per client at a time – Ovidiu Latcu Sep 21 '11 at 14:22
  • So the best thing to do is to have two different services running simultaneously launching to httprequest from two separate httpclient in the same time? – Romain Piel Sep 21 '11 at 14:26

3 Answers3

19

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

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • One more thing. Why using an ExecutorService and not multiple AsyncTasks? – Romain Piel Sep 21 '11 at 15:13
  • ES is an interface which has implementations with specific features, like scheduling or thread pool reusing. ES is designed to handle lots of tasks that get queued, etc.. It's probably more server oriented. So in your case AsyncTask is probably better. – Peter Knego Sep 21 '11 at 18:14
  • Also AsyncTask is speciallly designed to handle proper UI updating. – Peter Knego Sep 21 '11 at 18:15
  • Apache suggests against what you said http://pages.citebite.com/u4s3k8m9okfr. Do you have some data to support your answer? – Gaurav Agarwal Aug 21 '12 at 22:17
  • I am doing multiple async talk for handling server side push. But android blocks the requests. It allows only one connection to exists at a time. Do you have any work around? – Kunal Singh Sep 03 '14 at 14:29
5

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.

Håvard Geithus
  • 5,544
  • 7
  • 36
  • 51
0

Do some testing to determine how many concurrent HTTPRequests work well.

I recommend starting one service and having many Threads rather than multiple services.

Mike dg
  • 4,648
  • 2
  • 27
  • 26