Using AsyncTask to Run on a Background Thread
The next step is to call and test the networking code you just added. However, you cannot simply call FlickrFetchr.getURL(String) directly in PhotoGalleryFragment. Instead, you need to create a background thread and run your code there.
The easiest way to work with a background thread is with a utility class called AsyncTask. AsyncTask creates a background thread for you and runs the code in the doInBackground(…) method on that thread.
In PhotoGalleryFragment.java, add a new inner class called FetchItemsTask at the bottom of PhotoGalleryFragment. Override AsyncTask.doInBackground(…) to get data from a website and log it. Then use the new class inside of PhotoGalleryFragment.onCreate(…).
Listing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access