Title: How should I make multiple http requests in android
Tags: android
Question: Hey I am trying to make multiple http requests and wondering the best way to perform this in android. Currently I am using an IntentService with threads, however this doesnt work too well because onhandleintent returns before the threading is complete. Should I Switch to A regular service and start my own threads in there or would asyncTask be more approiate?
Thanks

Comment: Are you starting from a service?

Comment: Currently starting from an activity and I am calling an IntentService

Here is the accepted answer: Any time you deal with threads you're going to have to deal with synchronization. For example, when onHandleIntent() is called you may need to synchronize with your HTTP request thread(s) and wait for it to complete.

If you know ahead of time you will be doing this a lot it may be worth looking at something like ThreadPoolExecutor to save a bit on the pains of creating and tearing down threads. Again you will still need to synchronize your threads if you want to wait for their termination before moving on. Android has many ways of doing this including abstractions that make it fairly trivial to implement.

Comment for this answer: I am Starting 6 threads everytime the intentService is called. Each thread is responsible for making an http request with different xml to get different data back. Then I am taking the data and inserting it into a local sqlite database. My code looks something like the folowing:
http://pastebin.com/fGqXeEbS
Comment for this answer: I suggest you take a look at ThreadPoolExecutor (https://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html) if you will be doing this consistently. Either way this sounds better suited for an actual service.

Here is another answer: If you're already starting new threads anyway (whether directly or indirectly), then yes you should use a ```Service``` instead of an ```IntentService```. The whole point of an ```IntentService``` is for it to handle threading for you.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:03/12/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android>', 'Title': 'How should I make multiple http requests in android', 'ViewCount': 1266, 'id': 9672086, 'user_AboutMe': '', 'user_AccountId': 1210186.0, 'user_DisplayName': 'Tyler', 'user_Reputation': 137.0, 'user_Views': 7.0, 'user_id': 1178900.0}}