1

I have two AsyncTask, the first is for the http request #1 and the other is for another request. I'd like to call one after the other like:

new AsyncCallerTickets().execute();
new AsyncCallerContacts().execute();

Is it possible?

My Tasks are:

private class AsyncCallerTickets extends AsyncTask<Void, Void, Void>
{
    SpotsDialog dialog = new SpotsDialog(TicketsDetailsContacts.this, R.style.Custom);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog.setCancelable(false);
        dialog.setMessage(getString(R.string.dialog_wait));
        dialog.show();

    }
    @Override
    protected Void doInBackground(Void... params) {

        responseStringTickets = mRequestTickets(urlTickets);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dialog.dismiss();

               }

}

and

private class AsyncCallerContacts extends AsyncTask<Void, Void, Void>
{
    SpotsDialog dialog = new SpotsDialog(TicketsDetailsContacts.this, R.style.Custom);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog.setCancelable(false);
        dialog.setMessage(getString(R.string.dialog_wait));
        dialog.show();

    }
    @Override
    protected Void doInBackground(Void... params) {

        responseStringTickets = mRequestTickets(urlContacts);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dialog.dismiss();

               }

}

Can someone explain me how I can do? Thanks in advance.

Jigen
  • 465
  • 1
  • 7
  • 20
  • So for example can I use a timer to workaround? @R.Kirill – Jigen May 18 '16 at 07:46
  • 1
    why don't you just start a second task in `onPostExecute()` method of your first task? You can read more about AsyncTask in this [post](http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible) – x0r May 18 '16 at 07:48
  • @R.Kirill I tried before I read your answer and it's working, thanks you – Jigen May 18 '16 at 07:51
  • or just use ExecutorService with Runnable or ThreadPoolExecutor. You get a lot of flexibility. BlockingQueue ThreadPoolExecutor – Rafal May 18 '16 at 07:57
  • @Rafal Kirill response works for me, but can you explain me better your solutions please? – Jigen May 18 '16 at 08:08
  • @Jigen lets check this first before I put more code here: https://www.javacodegeeks.com/2011/12/using-threadpoolexecutor-to-parallelize.html – Rafal May 18 '16 at 08:18

3 Answers3

1

Use ExecutorService and Runnable instead of AsyncTask

ExecutorService schTaskEx = Executors.newFixedThreadPool(1);
schTaskEx.execute(r);
//more runnable : schTaskEx.execute(r1);
//more runnable : schTaskEx.execute(r2);
//etc

where r is your runnable.

   Runnable r = new Runnable() {
            public void run() { 
                //your code in backgroud thread is here 
            }
        };
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
1


You can try calling one after another by starting the second one in the callback method called in onPostExecute of the first async task.
Complete sketch-solution below:

public CustomActivity extends Activity{

    @Override
    void onCreate(Bundle saveInstance){
        super.onCreate(saveInstance);
        new AsyncCallerTickers(mCallback1).execute();
    }

    public Callback mCallback1 = new Callback{
       @Override
       public void onSuccess(){
           new AsyncCallerContacts().execute();
       }

      @Override
      public void onFail(){

      }
   }

   public interface Callback {
      void onSuccess();
      void onFail();
  }  

}

//asynctask classes

public class AsyncCallerTickets extends AsyncTask<Void, Void, Void> {

     SpotsDialog dialog = new SpotsDialog(TicketsDetailsContacts.this, R.style.Custom);
      private CustomActivity.Callback callback;
      public AsyncCallerTickets(CustomActivity.Callback _callback){
            this.callback = _callback;
      }  

      @Override
      protected void onPreExecute() {
         super.onPreExecute();

         dialog.setCancelable(false);
         dialog.setMessage(getString(R.string.dialog_wait));
         dialog.show();
      }

      @Override
      protected Void doInBackground(Void... params) {
          responseStringTickets = mRequestTickets(urlTickets);
          return null;
     }

     @Override
     protected void onPostExecute(Void result) {
          super.onPostExecute(result);
          dialog.dismiss();
          if(result!=null){
              callback.onSucces();
          }
          else{
               callback.onFail();
          }
     }
}

public class AsyncCallerContacts extends AsyncTask<Void, Void, Void> {
     SpotsDialog dialog = new SpotsDialog(TicketsDetailsContacts.this, R.style.Custom);

   @Override
   protected void onPreExecute() {
       super.onPreExecute();

       dialog.setCancelable(false);
       dialog.setMessage(getString(R.string.dialog_wait));
       dialog.show();
   }

   @Override
   protected Void doInBackground(Void... params) {

       responseStringTickets = mRequestTickets(urlContacts);
       return null;
   }

   @Override
   protected void onPostExecute(Void result) {
       super.onPostExecute(result);
       dialog.dismiss();              
   }

}


In general I like to have them in separate classes with some callback attached and in the Ui, where I start the task I put the callback, and when the async task returns with a success, I start the second. Keep it simple.

Andrei T
  • 2,985
  • 3
  • 21
  • 28
0

Try executing the 2nd AsyncTask in the onPostExecute() of the first AsyncTask as a workaround !!

Paras Jain
  • 49
  • 2