Android generische Asynctask

Ich habe derzeit mehrere Aktivitäten, die eine asynchrone Aufgabe für http-Post ausführen müssen, und ich möchte die asynchrone Aufgabe als eine andere Klassendatei ausführen, damit die andere Aktivität die asynchrone Aufgabe zum Ausführen der http-Post-Anforderung und von onPostExecute aufrufen kann. Rufen Sie die Methode httpResult (result) auf. in der Aktivität, die die asynctask aufgerufen hat. Ich habe versucht, die Aktivität weiterzuleiten, kann die Methode jedoch nicht in onPostExecute aufrufen. Wie kann ich das machen?

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


    }
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage