0

I'm implementing a class which extends AsyncTask and I perform an http request within this class. The class is not an Activity and is located in a seperate java file because I want to use this class several times.

I instantiate an object of this class in my Activity, to execute the http request in a separate thread. When the thread executes, I want to call a method of my Activity.

How do I implement this? I need the result of the http request in my Activity but I can't handle this so far.

This is the code for the thread task...

public class PostRequest extends AsyncTask<String, Void, String> {
public String result = "";

@Override
protected String doInBackground(String... urls) {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://bla/index.php?" + urls[0]);
        // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();

        // convert response to string
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

@Override
protected void onPostExecute(String result) {

}
}

And this is part of my Activity code that creates the thread class...

public class ListActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);


    PostRequest task = new PostRequest();
    task.execute(new String[] { "action=getUsers" });
    task.onPostExecute(task.result) {

    }
}

public void Display(String result) {
    try {
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data = jArray.getJSONObject(0);
        String value = json_data.getString("name");
        TextView text = (TextView) findViewById(R.id.value);
        text.setText(value);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Phil123
  • 87
  • 11

3 Answers3

1

pass the activity reference in constructor......

as

public class PostRequest extends AsyncTask<String, Void, String> {
public String result = "";
private Activity mActivity;

 public PostRequest(Activity activity){
          super();
         mActivity = activity;
}

......
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • But how do i now access the method of this activity? I've tried to do this like mActivity.Display(); but this doesn't work... Thanks so far! – Phil123 Jun 05 '12 at 18:48
  • 1
    either use you activty name in place of the Activity in PostRequest function of just type cast activity to your activity.... – Dheeresh Singh Jun 11 '12 at 16:48
  • @DheereshSingh, thanks, I thought that typecasting to Activity should be enough but you're right, it should be typecasted to real activity class. – gorodezkiy Oct 22 '13 at 22:11
0

You don't have to do a onPostExecute() as this is called after the process doInBackground has completed and then you can use the reference of the activity passed into the constructor of the AsyncTask to run any time of method on your UI.

Just remember that onPostExecute() method runs on a UI thread so here from this method you can try to modify your view if needed.

Sana
  • 9,895
  • 15
  • 59
  • 87
0

See this question...can-i-put-asynctask-in-a-separate-class-and-have-a-callback and the accepted answer. If you want a re-usable AysncTask as a stand-alone class then using a listener as a callback for all of your activities is the best way to do it.

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135