Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Johan Rönkkö
Johan Rönkkö
28,054 Points

how to fetch data with asyncktask?

Hello! I have an assignment where I should use asynctask to fetch json data. Our teachers have told us to use this link: https://gist.github.com/anonymous/1c04bf2423579e9d2dcd for doInBackground and helped us setup a basic onPostExecute. The json data is going to be fetched from this link: http://wwwlab.iit.his.se/brom/kurser/mobilprog/jsonservice.php (it's just som random json). The assignment is about fetching the json and putting it into our listview. my problem is to fetch the jsondata? i know the basics of how to get the jsonobject, jsonarrays etc, but not where i should fetch it? what should i do in doInbackground and what should i do in onPostExecute?

2 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

You can put all the code for parsing the JSON object in a private function, here I'm calling it getDataFromJson, and then call it at the end of doInBackground(), after that finally block in your sample code:

try {
    return getDataFromJson(forecastJsonStr);
} catch (JSONException e) {
    e.printStackTrace();
}
 // Return null in case if there was an error getting or parsing the json
return null;
Johan Rönkkö
Johan Rönkkö
28,054 Points

Thanks alot! I guess the function would be something like: private JSONObject(String) { //all of the code from the link } ?

Kourosh Raeen
Kourosh Raeen
23,733 Points

Actually, none of the code from the link goes into that function. The code from the link goes into doInBackground() , which is overridden inside your custom class extending AsyncTask. What goes inside getDataFromJson(forecastJsonStr) is the code you need to write to do the parsing.

Johan Rönkkö
Johan Rönkkö
28,054 Points

ahh i see i'll have to do two try-catch block after eachother inside of doInBackground, then ill just add the json to my onPostExecute

Kourosh Raeen
Kourosh Raeen
23,733 Points

Yes. You can add the code for onPostExecute() after doInBackground() and there you get the result, parsed JSON, and add it to your adapter. Something like:

@Override
 protected void onPostExecute(String[] result) {
    if (result != null) {
        myAdapter.clear();
        for(String str : result) {
            myAdapter.add(str);
        }
    }
}
Johan Rönkkö
Johan Rönkkö
28,054 Points

thanks alot for the help!

Kourosh Raeen
Kourosh Raeen
23,733 Points

Extend the AsyncTask class. Then in doInbackground() make the connection and parse the JSON. Then the parsed result will be available in onPostExecute(). That's where you add it to the adapter so your ListView gets updated.

Johan Rönkkö
Johan Rönkkö
28,054 Points

ahh i see, but how do i parse my json, or what does it refer to? and where in doInBackground should i parse the json? what line number if you look at this code: https://gist.github.com/anonymous/1c04bf2423579e9d2dcd