将 void 设置为 AsyncTask 并在 doInBackground 完成后执行它

set void to AsyncTask and execute it after doInBackground finishes

我正在尝试使用 AsyncTask 在 Android Studio 中发出 HTTP GET 请求。此 AsyncTask class 用于应用程序的许多活动。我想为 AsyncTask 分配一个 void 函数,它将在 doInBackground 获取 HTTP GET 请求后执行。应将 HTTP 返回的响应插入到 void 的参数中。我已经研究了很多次,但找不到一个简单的工作解决方案。需要帮忙。谢谢。

Main.java 包含 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 使用 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.
    }
}

向您的 AsyncTask 添加结果回调并处理结果:

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);
    }
}

然后在您的 activity 中调用您的函数:

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