In last week’s tip, David described how to use the open-source library Retrofit to make web service calls. In recent versions of Android, you have to make these calls asynchronously. David solved that requirement last week by wrapping the call in an AsyncTask object. But Retrofit actually has tools you can use to make the call asynchronously without an AsyncTask, an IntentService, or other sort of separate structure.
- [Instructor] In last week's tip, I described how to use the open source library Retrofit to make web service calls. In recent versions of Android, you have to make these calls asynchronously. I solved that challenge last week by creating an AsyncTask that receives the request, executes the web service call in the background and then handles the response in the onPostExecute method, which is executed in the main thread. But Retrofit actually has tools you can use to make the calls asynchronously without an AsyncTask, an IntentService or any other sort of separate structure.
Let's start with the web service. The web service call doesn't change at all. You still create a Retrofit object that's accessible from outside the class and then you define your call using a get or a post annotation. In this example, I'm using a get request to retrieve all data items from the JSON feed. In my current code, I've wrapped the request inside the web service task, AsyncTask class. When I call the execute method, that results in a synchronous call and I'm returning the data directly from that call.
That's what's now going to change. I'll go my RunCode method and I'll delete these two lines that are creating and running the AsyncTask. Now I'm going to borrow some code from that AsyncTask. I'll come down here and I'll select these two lines and I'll copy them, I'll go back to the RunCode method and I'll paste. I'm still creating the call object and not yet executing it. Now instead of using the execute method, I'll use a method named enqueue.
It looks like this, call.enqueue. This method receives an object called a callback, and a callback is a member of the Retrofit library. I'll create that as an anonymous implementation and when I select it from the list, Android Studio automatically generates an on response and an on failure method. If the on response method is called, that means there's something to process and the first thing I need to do is make sure the call was successful.
You can do that with a conditional block and you can look at the response object and call the method is successful and if that's true, then you'll have data to work with. I'm going to respond the same way I did in the previous tip. I'll loop through the data and display it on the screen. So I'll come down here to my onPostExecute method and I'll copy that code, then I'll return up here and I'll paste it into place. And I'll change the name of the collection of data that I'm working with.
It's no longer called Data Items. I'm getting a response object and from there, I'll call the method Body. The Body method returns my collection of data items. Next, I'll implement the on failure method and all I'm going to to do here is log some output and I'll say there was a failure. Now there are a number of error indicators here, but they're easy to fix. They're just warnings that there are certain annotations that might be useful. You can go through each of these and add the annotations with intention actions.
And now, I'm no longer using that AsyncTask, so I'll come down here and I'll delete it. Then I'll select optimize imports and clean up the code. And I'm ready to test. I'll clear the log and click RunCode and there's the result. I've made the call to the remote service and I've received the response. So now the Retrofit code is all in one place, instead of being scattered between the activity and either an AsyncTask or an IntentService.
The call and the handling of the result is still being done in the background thread as required, but I no longer have to create a completely different class to manage it.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.