Android 通用异步任务 [英] Android generic Asynctask

查看:15
本文介绍了Android 通用异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I currently have multiple activity that needs to perform an asynctask for http post and I wish to make the asynctask as another class file so that the different activity can call the asynctask to perform the http post request and onPostExecute, call the method httpResult(result) in the activity that called the asynctask. I have tried to pass the activity in but I am unable to call the method in onPostExecute. How can I do that?

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

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 的接口并添加所需的方法 httpResult

Create an Interface called HttpResponseImpl in a seperate file and add the required method httpResult

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

现在由你的Activity类实现这个接口

Now implement this interface by you Activity class

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 类将是.

And your AsyncHttpPost class would be.

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


    }
}

使用您要从中执行 HttpRequest 的所有 Activity 类实现此 HttpResponseImpl 接口.

Implements this HttpResponseImpl interface with all you Activity class from which you want to do HttpRequest.

这篇关于Android 通用异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆