Does Android support multiple HTTP requests at the same time?
Asked Answered
V

3

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!

Visual answered 21/9, 2011 at 14:19 Comment(2)
I don't think there is any other way. You can make just one httprequest per client at a timeCairngorm
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?Visual
O
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

Olimpiaolin answered 21/9, 2011 at 14:33 Comment(5)
One more thing. Why using an ExecutorService and not multiple AsyncTasks?Visual
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.Olimpiaolin
Also AsyncTask is speciallly designed to handle proper UI updating.Olimpiaolin
Apache suggests against what you said pages.citebite.com/u4s3k8m9okfr. Do you have some data to support your answer?Marge
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?Coussoule
P
4

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.

Pichardo answered 6/2, 2012 at 20:52 Comment(2)
Fixed link, it is not yet deprecated in the Android platform.District
Updated answer to reflect current status of the android API.District
F
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.

Fredkin answered 21/9, 2011 at 14:26 Comment(1)
Interesting idea. Concurrent HttpClient running one HttpRequest each then, right?Visual

© 2022 - 2024 — McMap. All rights reserved.