8

I am quite new to Android Development. I am working in a app where i need to make a lot of async calls to api. For each and every API call, I have to write the similar looking AsyncTask class over and over again. So, is there any way to make these Async Calls modular?

Milan Maharjan
  • 4,156
  • 1
  • 21
  • 28

3 Answers3

8

yes it is possible use Listener and AsyncTask with parameterized constructor

Check it

Create an interface

public interface OnTaskCompletListerner {
 void oncompleteListerner(String name);
}

create an AsyncTask class as follows

public class AsyncTaskModuler  extends AsyncTask<Void, Void, String> {

    private Context context;
    private HashMap<String, String> data;
    private String URL;
    private OnTaskCompletListerner taskdone;

    public AsyncTaskModuler(Context ctx,HashMap<String, String> data,String url,OnTaskCompletListerner taskdone){

        this.context=ctx;
        this.data=data;
        this.URL=url;
        this.taskdone=taskdone;
    }

    @Override
    protected String doInBackground(Void... params) {
        //Do the task here and return the value if needed 
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        taskdone.oncompleteListerner(result);
    }

and call it in your activity like this

public class CallAync extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url="";
        HashMap<String, String> data=new HashMap<String, String>();
        AsyncTaskModuler moduler=new AsyncTaskModuler(CallAync.this, data, url, completListerner);
        moduler.execute();
    }

    OnTaskCompletListerner completListerner=new OnTaskCompletListerner() {

        @Override
        public void oncompleteListerner(String name) {

        }
    };
}
Milan Maharjan
  • 4,156
  • 1
  • 21
  • 28
Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18
1

Create an general async task and pass your url as param to it .Hence you can reuse the same async task for all your api calls

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

    String muUrl;   


    public myTask(Context context, Activity activity,
            String url) {
        contxt = context;
        myUrl=url;
    }

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

        makeApiCalls();

        return null;
    }
}

Start the task in the following way :

new myTask(this, this, urlStr).execute();

EDIT

How can I perform different logic onPostExecute() ?

You can add another param in the constructor of myTask. Ex.

    public MyTask(Context context, Activity activity,
                    String url,String postExecuteAction) {
}

In your post executes just check of each case in if else and perform the respective task

Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
0

Dummy Background class

 private class BackGroundClass extends AsyncTask<String, Void, Bitmap> //<arg to do InBackground,,returntype of do inBackground and arg of onPostExecut>
   
{

 public BackGroundClass()
    {

    }

 @Override
    protected void onPreExecute() //forground work in UI thread prior to doInbackground
    {
        super.onPreExecute();
      
    }

    @Override
    protected Bitmap doInBackground(String... urls) //background work in parallel thread
    {
        Bitmap b=null;
       // your background work
        return b;
    }

    @Override
    protected void onPostExecute(Bitmap result) //forground work in UI thread post to doInbackground
    {
        super.onPostExecute(result);
        if(result!=null)
        {               
            //use bitmap image in result    
        }
       else
       {   
          //Image is not available
       }    

    }

  }

Calling for one parellel execution

new BackGroundClass().execute(StringArg1,stringArg2,StringArg3);

Calling for multiple parellel execution

when tou need to call more than one background task at same time

new BackGroundClass().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,StringArg1,stringArg2,StringArg3);

Called from single activity

use this as an inner class to your activity for making it easy to share variable if not called from more than one activity

Called from more than one activity

use constructor to pass activity context,and other variable

new BackGroundClass(constructor arguments).execute(StringArg1,stringArg2,StringArg3);

  new BackGroundClass(constructor arguments).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,StringArg1,stringArg2,StringArg3);
Community
  • 1
  • 1
Fahad
  • 193
  • 1
  • 9