3

I have been trying to create a simple app that POSTS to a PHP server. I would like to create a single class that can handle all post requests and then return data back to the original activity.

Presently I have a class called ConectionHandler which is designed to handle all of the Post requests.

public class ConnectionHandler extends AsyncTask<String, Void, String> {

public ConnectionHandler(ArrayList targetList, ArrayList dataList, String hostLocation){
    ...
}

protected String doInBackground(String... params){
    ...
    return returnData;
}

I have been creating an object ConnectionHandler from my loginActivity. Once I receive the data in the doInBackground method I want it to go back to the loginActivity so that I can process it and start a new activity.

Is there any simple way to do this? I have experimented with many different ideas and none seem to work so far.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hirsh
  • 186
  • 1
  • 8

1 Answers1

2

Have you tried using the onPostExecute method in the AsyncTask? When you return a value from doInBackground, it is sent to onPostExecute which runs on the main UI thread.

public class ConnectionHandler extends AsyncTask<String, Void, String> {

    public ConnectionHandler(ArrayList targetList, ArrayList dataList, String hostLocation){
        ...
    }

    protected String doInBackground(String... params){
        ...
        return returnData;
    }

    @Override
    protected void onPostExecute(String result) {
       //do whatever you want with the data here 
    }    
}
Rich Luick
  • 2,354
  • 22
  • 35
  • I did actually try that. I can't figure out how to reference the loginActivity for things like "getApplicationContext". Is there a way to make that work? – Hirsh May 26 '15 at 01:34
  • So you are having an issue with the context? You can pass that in to the async task via the constructor – Rich Luick May 26 '15 at 01:35
  • Can I start an intent from the onPostExecute method? I have been trying to do so, but I don't know what to pass in as the first parameter. If there's a way to do this it would be way better than using application context. Thanks! – Hirsh May 26 '15 at 02:10
  • Try CurrentActivity.this for the first parameter where CurrentActivity is the name of the activity you are in – Rich Luick May 26 '15 at 02:28