Get returned JSON from AsyncTask

12,292

The method onPostExecute has as parameter the JSONArray you returned from the doInBackground method.

onPostExecute runs on your main (caller) activity's thread, so besides dismissing the dialog in that method, you can process the result array further, pass it safely to other methods, etc:

@Override
protected void onPostExecute(JSONArray result)
{
    super.onPostExecute(result);
    final Message msg = new Message();
    msg.obj = result;
    if (youWantToUseHandler)
        handler.dispatchMessage(msg);
    else
        writeJSONArray(result);
}

the handler:

final Handler handler = new Handler()
{
    public void handleMessage(Message msg) 
    {
        final JSONArray result = (JSONArray)msg.obj;
        writeJSONArray(result);
    };
};

Some other method:

private void writeJSONArray(final JSONArray result)
{
    for (int i = 0; i < result.length(); i++)
    {
        try
        {
            Log.d("SAMPLE", result.get(i).toString());
        }
        catch (JSONException e)
        {
            Log.e("SAMPLE", "error getting result " + i, e);
        }
    }
}

Since onPostExecute "Runs on the UI thread after doInBackground. The specified result is the value returned by doInBackground or null if the task was cancelled or an exception occured." ~API Docs You can call any method you've declared in your class, and pass this array as a parameter to it.

Share:
12,292
simtaxman
Author by

simtaxman

Updated on June 04, 2022

Comments

  • simtaxman
    simtaxman about 2 years

    So I have this loader class which extends AsyncTask. Then I do new loader().execute(); but I want to use the JSONArray response which my loader class returns how do I do that? Because I need it in several different places? Or should I just move my code to onPostExecute and do everything from there?

    public class loader extends AsyncTask<String, Integer, JSONArray> {
    
        ProgressDialog dialog;
    
        protected void onPreExecute() {
    
            dialog = ProgressDialog.show(ChallengeList.this, "", "Laddar...");
            dialog.setCancelable(true);
        }
    
        @Override
        protected JSONArray doInBackground(String... params) {
    
    
        JSONArray response = null;
        HttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(listURL);
    
        try {
    
            HttpResponse resp = client.execute(httppost);
            StatusLine statusLine = resp.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            Log.i("Statuscode", "statusCode"+statusCode);
            if (statusCode == 200) {
                final JSONObject json = new JSONObject();
    
                json.put("userID", prefs.id());
    
                response = SendHttp.parseHttp(listURL, json);
    
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } 
    
            return response;
        }
        protected void onPostExecute(JSONArray result) {
            dialog.dismiss();
        }
    }