For multiple request you can use ThreadPoolExecutor
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new callApi().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
new callApi().execute(location);
}
Thread Pool Pattern
AsyncTask uses a thread pool pattern for running the stuff from doInBackground()
The Thread pool Pattern is where number of Threads are created to perform a number of Tasks. It is basically a container where multiple threads come in a queue for different task.
For Example:
public class MultipleAsyncTask extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
runMultipleAsyncTask(); // Start Async Task
}
private void runMultipleAsyncTask() // Run Multiple Async Task
{
FirstAsyncTask asyncTask = new FirstAsyncTask(); // First
if(AppUtil.isCurrentVersionHoneycombAndAbove()) // Above Api Level 13
{
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else // Below Api Level 13
{
asyncTask.execute();
}
SecondAsyncTask asyncTask2 = new SecondAsyncTask(); // Second
if(AppUtil.isCurrentVersionHoneycombAndAbove())// Above Api Level 13
{
asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else // Below Api Level 13
{
asyncTask2.execute();
}
}
//Start First Async Task:
private class FirstAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
Log.i("AsyncTask" ,"FirstOnPreExecute()");
}
@Override
protected Void doInBackground(Void... params)
{
for(int index = 0; index < 50; index++)
{
Log.i("AsyncTask" ,"FirstAsyncTask");
try
{
Thread.sleep(100);
}
catch (InterruptedException exception)
{
exception.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
Log.d("AsyncTask" ,"FirstonPostExecute()");
}
}
//Start Second Async Task:
private class SecondAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
Log.i("AsyncTask" ,"SecondOnPreExecute()");
}
@Override
protected Void doInBackground(Void... params)
{
for(int index = 0; index < 50; index++)
{
Log.d("AsyncTask" ,"SecondAsyncTask");
try
{
Thread.sleep(100);
}
catch (InterruptedException exception)
{
exception.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
Log.d("AsyncTask" ,"SecondOnPostExecute()");
}
}
}
Output:
FirstOnPreExecute()
SecondOnPreExecute()
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstonPostExecute()
SecondOnPostExecute()
Same asyncTask for different functions like api requests:
boolean flag;
@Override
protected String doInBackground (String... params) {
flag= params[0].endsWith("/repos");
//other statements
}
Now in your onPostExecute:
if(flag){
//parse one way
} else {
//parse another way
}