TrinityTuts

Reuse AsyncTask for multiple web calls android

Last updated on January 5th, 2020 at 08:53 pm

Last year i create a method through which i can make multiple web call with single Async class. I am keep trying to update and some feature in that. At this time i make some changes in it and add some more functionality which save your time.

 

  1. Now no need to add extra check for internet connection.
  2. You can turn on/off dialog box.
  3. Set custom text over dialog box easily.
  4. Make GET/POST request from same class.
  5. No external lib required.

I upload my complete code to git if you like to add some thing in it which help people go ahead.

Git Link: https://github.com/anehkumar/AsyncReuse.

If you never tried this class before i will explain you in simple step how you can use this in your project.

Step 1. Create new project in your Android studio.

Step 2. Download my sample code from git.

Step 3. After download my code go to AsyncReuse/app/src/main/java/com/sampleasyncreuse/ in your downloaded code and copy AsyncReuse.java and GetResponse.java in your project. These two class are main through which you can make call to your web services and get response from that.

Step 4. Open that class where you want to send data to server and implement GetResponse interface and implement its method. As shown below code.

package com.sampleasyncreuse;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements GetResponse {

    private AsyncReuse requestServer;
    private CheckInternet checkInternet;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        executeServerReq();

        // Add your post and get param in this method eg:requestServer.getObjectQ("fname=aneh&lname=thakur");
        requestServer.getObjectQ("key=value&key1=value1");  
        requestServer.getRequestMethod("POST"); // Set you request method GET/POST
        requestServer.setDialogtext("Loading data from server"); // Set your dialog box text on loading data
        requestServer.execute();
    }

    private void executeServerReq() {
        requestServer = new AsyncReuse(URLs.listURL, true, this, getApplicationContext());
        requestServer.getResponse = this;
    }

    @Override
    public void getData(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response.toString());
            Log.e("here", "----------------" + jsonObject.toString());
            TextView textView = (TextView) findViewById(R.id.opt);
            textView.setText(jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

After implementation of interface you need to create instance of AsyncReuse class. I create a separate method in which i create object of my AsyncReuse class and pass required parameter in it.

 private void executeServerReq() {
        requestServer = new AsyncReuse(URLs.listURL, true, this, getApplicationContext());
        requestServer.getResponse = this;
    }

You can pass your parameter using AsyncReuse Object and also set other option as shown below

 // Add your post and get param in this method eg:requestServer.getObjectQ("fname=aneh&lname=thakur");
        requestServer.getObjectQ("key=value&key1=value1");  
        requestServer.getRequestMethod("POST"); // Set you request method GET/POST
        requestServer.setDialogtext("Loading data from server"); // Set your dialog box text on loading data
        requestServer.execute();

After setting all this when you execute this, you get your response inside

@Override
    public void getData(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response.toString());
            Log.e("here", "----------------" + jsonObject.toString());
            TextView textView = (TextView) findViewById(R.id.opt);
            textView.setText(jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

getData() method. Hope this will help you. Please let me know if you implement some new and better thing in above code 🙂