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;
}
1
  • gives an error? ==> Look at the Logcat or provide here. Commented Apr 1, 2013 at 9:35

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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<String> ret = sample.getData(); It again gives errors, how to retreive/send values to different class from onpostexecute()?
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 stackoverflow.com/questions/5374546/…. To pass objects androidhub.wordpress.com/2011/08/03/…
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);
        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.