simtaxman
simtaxman

Reputation: 623

Get returned JSON from AsyncTask

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();
    }
}

Upvotes: 3

Views: 12410

Answers (2)

Nesh
Nesh

Reputation: 138

After downloading the content web you can use the code below in onPostExecute method:

protected void onPostExecute(String s) { super.onPostExecute(s);

        try {
            JSONObject jsonObject = new JSONObject(s);

            String weatherInfo = jsonObject.getString("weather");

            Log.i("Weather content", weatherInfo);

            JSONArray arr = new JSONArray(weatherInfo);

            for (int i=0; i < arr.length(); i++) {
                JSONObject jsonPart = arr.getJSONObject(i);

                Log.i("main",jsonPart.getString("main"));
                Log.i("description",jsonPart.getString("description"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Upvotes: 0

rekaszeru
rekaszeru

Reputation: 19220

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.

Upvotes: 4

Related Questions