Android 通用异步任务

标签 android android-asynctask

我目前有多个 Activity 需要为 http post 执行异步任务,我希望将异步任务作为另一个类文件,以便不同的 Activity 可以调用异步任务来执行 http post 请求和 onPostExecute,调用方法 httpResult (结果)在调用异步任务的 Activity 中。我试图传递 Activity ,但无法调用 onPostExecute 中的方法。我该怎么做?

public class MyActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_MyActivity);

    //logic here...
    AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
    JSONObject data = new JSONObject();
    data.put("key", "value");
    try {
            asyncHttpPost.execute(data);
        }
        catch (Exception ex){
        ex.printStackTrace();
        }
    }
    public static void httpResult(String result) {
        //this method has to get called by asynctask to make changes to the UI
     }

}

AsyncHttpPost.java

public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
String recvdjson;
String mData="";
private ProgressDialog dialog;
private Activity activity;


public AsyncHttpPost(String data, Activity activity, ProgressDialog dialog) {
    mData = data;
    this.activity = activity;
    this.dialog = dialog;
}

protected void onPreExecute()
{

    dialog.show();
}

    @Override
    protected String doInBackground(JSONObject... params) {

       //logic to do http request
           return "someStringResult";

    }

    protected void onPostExecute(String result) {
        dialog.dismiss();
        activity.httpResult(result); //This line gives me error : The method httpResult(String) is undefined for the type Activity


    }
}

最佳答案

在单独的文件中创建一个名为 HttpResponseImpl 的接口(interface)并添加所需的方法 httpResult

interface HttpResponseImpl{
    public void httpResult(String result);
}

现在通过你的Activity类实现这个接口(interface)

public class MyActivity extends Activity implements HttpResponseImpl{

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_MyActivity);

    //logic here...
    AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
    JSONObject data = new JSONObject();
    data.put("key", "value");
    try {
            asyncHttpPost.execute(data);
        }
        catch (Exception ex){
        ex.printStackTrace();
        }
    }

    public void httpResult(String result){
        //this method has to get called by asynctask to make changes to the UI
    }
}

您的 AsyncHttpPost 类将是。

public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
String recvdjson;
String mData="";
private ProgressDialog dialog;
private HttpResponseImpl httpResponseImpl;


public AsyncHttpPost(String data, HttpResponseImpl httpResponseImpl, ProgressDialog dialog) {
    mData = data;
    this.httpResponseImpl = httpResponseImpl;
    this.dialog = dialog;
}

protected void onPreExecute()
{

    dialog.show();
}

    @Override
    protected String doInBackground(JSONObject... params) {

       //logic to do http request
           return "someStringResult";

    }

    protected void onPostExecute(String result) {
        dialog.dismiss();
        httpResponseImpl.httpResult(result); 


    }
}

实现此 HttpResponseImpl 接口(interface)与您要从中执行 HttpRequest 的所有 Activity 类。

关于Android 通用异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12506916/

相关文章:

android - 停止定期运行 AsyncTask

android - Activity 丢失后终止异步任务

android - Youtube 视频未在 android 的 webview 中加载

android - 更改 Material 组件样式属性失败

android - GpsStatus 监听器 : no satellites used in fix although status is GpsStatus. GPS_EVENT_FIRST_FIX

android - requestLocationUpdates 在一些距离改变/行进时获取更新

java - finish() 方法在 AsyncTask 内部不起作用

android - 如何在 Android 的 viewpager 的不可见 fragment 中执行一个函数

javascript - 发送 GET 请求,同时读取流

java - 如何在 Android 中最好地存储和修改分类数据