title: Calling method of activitiy from a non-activity class in android
body: <p>I'm implementing a class which <code>extends AsyncTask</code> and I perform an http request within this class. The class is not an <code>Activity</code> and is located in a seperate java file because I want to use this class several times.</p>
  
  <p>I instantiate an object of this class in my <code>Activity</code>, to execute the http request in a separate thread. When the thread executes, I want to call a method of my <code>Activity</code>.</p>
  
  <p>How do I implement this? I need the result of the http request in my <code>Activity</code> but I can't handle this so far.</p>
  
  <p>This is the code for the thread task...</p>
  
  <pre><code>public class PostRequest extends AsyncTask&lt;String, Void, String&gt; {
  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) {
  
  }
  }
  </code></pre>
  
  <p>And this is part of my <code>Activity</code> code that creates the thread class...</p>
  
  <pre><code>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();
      }
  }
  }
  </code></pre>