0

I have a requirement where i need to parse the content of a URL in JSON format. I am able to do that successfully. But i need to save the contents of the URL in a array list and pass them back to the calling functions. Below is the code snippet of what i am trying to achieve.

@Override
protected ArrayList<String> onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        dialog.dismiss();
        return ar; // ar is the arraylist i have created and updated it with the content of the url.
    }

But running this gives an error. Can anyone please suggest how i can make this possible. However, when i make the return type of onPostExecute as void and toast the contents, its displaying properly. When i call this after the execute, its returning null even though i have updated the contents in doinbackground(). Hence i am unable to get the return values on arraylist format.

// Calling function
Myadapter.execute();
ArrayList<string> str = new ArrayList<string>();
str = print();
// Here str is getting null

// Called function
    public ArrayList<String> print() {
    ArrayList<String> names = new ArrayList<String>();
    for(int i=0;i<al.size();i++)
    {           
        names.add(al.get(i).getConstituencyName());         
    }
    return names;
}
Rajitha Siriwardena
  • 2,749
  • 1
  • 18
  • 22
DroidLearner
  • 2,115
  • 5
  • 31
  • 50

2 Answers2

3

Use a handler

In your activity

 mHandler = new Handler() { 
   @Override public void handleMessage(Message msg) { 
      ArrayList s=(ArrayList)msg.obj;
      tv.setText("Result = "+s.get(0));

    }
  };    

In your onPostexecute

 Message msg=new Message();
 msg.obj=ar;
 mHandler.sendMessage(msg);
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Hi, thanks for the reply, this works if i have to add this to the same class, but if i have to create a wrapper class, and get the array as a return value, consider like JSONActivity sample = new JSONActivity(); ArrayList ret = sample.getData(); It again gives errors, how to retreive/send values to different class from onpostexecute()? – DroidLearner Apr 01 '13 at 10:04
  • once you return value and retreive ArrayList s=(ArrayList)msg.obj; pass the value s.get(0)); to the class constructor or to an activity using intent http://stackoverflow.com/questions/5374546/passing-arraylist-through-intent. To pass objects http://androidhub.wordpress.com/2011/08/03/android-intents-for-passing-data-between-activities-part-3/ – Raghunandan Apr 01 '13 at 10:06
1

The proper way would be to let your activity implement an interface, and when you instantiate the AsyncTask pass the current activity as a parameter to the constructor. Then in onPostExecute() invoke the callback method defined in the Activity and pass the json result as an argument.

Something like this:

interface OnTaskFinished {
    void onTaskFinished(String result);
}

public class MainActivity extends Activity implements OnTaskFinished {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ....
        new MyAsyncTask(this).execute();

    }

    @Override
    public void onTaskFinished(String result) {
        // Process the json result here how you need.
    }
}

And this is how the scheleton of your AsyncTask should look like:

private class MyAsyncTask extends AsyncTask<Void, Void, String> {

        private final OnTaskFinished listener;

        public MyAsyncTask(OnTaskFinished listener) {
            this.listener = listener;
        }

        // ...

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            listener.onTaskFinished(result);
        }

    }
Andy Res
  • 15,963
  • 5
  • 60
  • 96