Search code examples
javaandroidandroid-asynctask

set void to AsyncTask and execute it after doInBackground finishes


I'm trying to use AsyncTask to make HTTP GET requests in Android Studio. This AsyncTask class is used across many activities of the app. I want to assign a void function to the AsyncTask which will execute after the doInBackground has fetched the HTTP GET request. The HTTP returned response should be inserted into the void's parameter. I have researched a lot of times but cannot find a simple working solution. Need Help. Thanks.

Main.java containing the AsyncTask class:

package com.example.NAME;

import android.os.AsyncTask;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

    public static class GetData extends AsyncTask<String, Void, Boolean> {

        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected String doInBackground(String... params) {
            Boolean _return = false;
            
            try {
                String data = params[0];
                
                // DO THE HTTP GET REQUEST AND EVALUATE THE BOOLEAN RETURN VALUE
                _return = ???;
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            return _return;
        }

        @Override
        protected void onPostExecute(Boolean _return) {
            super.onPostExecute(_return);
        }
    }
}

Activity1.java using the AsyncTask class:

package com.example.NAME;

import android.app.Activity;
import android.os.Bundle;

public class Activity1 extends Activity {
    private final Main main = new Main();

    @Override
    protected synchronized void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity1);
    }

    public synchronized void GET_Request(View v) {
        Main.GetData t = new Main.GetData();
        t.execute("STRING");
        /*
          How to attach the `after_GET_Request` void that should be executed
          after the HTTP GET Request happens and should receive the returned
          Boolean value.
        */
    }
    
    public synchronized void after_GET_Request(Boolean b) {
        // Use the Boolean data received from AsyncTask.
    }
}

Solution

  • Add a result callback to your AsyncTask and process the outcome:

    public static class GetData extends AsyncTask<String, Void, Boolean> {
        private ResultCallback mCallback;
    
        public GetData(ResultCallback callback) {
            mCallback = callback;
        }
    
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        protected String doInBackground(String... params) {
            Boolean _return = false;
    
            try {
                String data = params[0];
    
                // DO THE HTTP GET REQUEST AND EVALUATE THE BOOLEAN RETURN VALUE
                _return = ???;
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return _return;
        }
    
        @Override
        protected void onPostExecute(Boolean _return) {
            super.onPostExecute(_return);
            mCallback.onResultReady(_return);
        }
    
        public interface ResultCallback {
            void onResultReady(Boolean result);
        }
    }
    

    And then in your activity call your function:

    Main.GetData t = new Main.GetData(new ResultCallback() {
        @Override
        public void onResultReady(Boolean result) {
            after_GET_Request(result);
        }
    });
    t.execute("STRING");