Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 25  HTTP and Background Tasks


The parseItems(...) method needs a List and JSONObject. Update fetchItems() to call
parseItems(...) and return a List of GalleryItems.


Listing 25.13  Calling parseItems(...) (FlickrFetchr.java)


public void List fetchItems() {


List items = new ArrayList<>();


try {
String url = ...;
String jsonString = getUrlString(url);
Log.i(TAG, "Received JSON: " + jsonString);
JSONObject jsonBody = new JSONObject(jsonString);
parseItems(items, jsonBody);
} catch (JSONException je) {
Log.e(TAG, "Failed to parse JSON", je);
} catch (IOException ioe) {
Log.e(TAG, "Failed to fetch items", ioe);
}


return items;
}


Run PhotoGallery to test your JSON parsing code. PhotoGallery has no way of reporting the contents
of your List right now, so you will need to set a breakpoint and use the debugger if you want to make
sure everything worked correctly.

Free download pdf