Making Our Code Asynchronous7:00 with Ken Alger
The OkHttp documentation includes an example of how to turn a synchronous call into an asynchronous one. In this video, we'll work through that process and verify that our background work completes successfully.
If we have another look at the recipes in the OkHttp documentation, 0:00 we recall that we are currently using this Synchronous Get Recipe in our code. 0:05 With some additional knowledge now about Threads, Concurrency, and Asynchronous 0:10 processing, we'll want to switch to this other recipe, the Asynchronous Get. 0:14 Notice here in the description that it will download a file on a worker thread 0:23 and get called back when the response is readable. 0:27 Let's update our code to be more like this Asynchronous model, 0:30 and then we'll talk about this call back idea. 0:33 So let's head back to Android Studio. 0:37 And main activity low, close the log. 0:42 So inside on create, we can keep a lot of our code the same. 0:46 The first difference is here, the response = call.execute. 0:50 This is the synchronous method. 0:57 We can delete that now. 0:59 So let's ignore this try catch block just for a moment, and here, 1:01 under where our call is initialized, 1:06 we want to add a new line to use the Asynchronous method, enqueue. 1:08 So call.enqueue. 1:13 Notice the parameter for this method is a callback object. 1:18 Type in new callback and hit enter to generate the callback. 1:21 Isn't auto complete great? 1:27 Well, sometimes, so this should look familiar. 1:29 We have an anonymous interclass of type callback. 1:32 It has two methods that we need to override, onFailure and onResponse. 1:36 Fortunately, good naming conventions have been used here, 1:41 making it easy to imagine what they're used for. 1:44 Let's turn back to our try-catch block. 1:46 We need to cut and paste it, but where to put it. 1:51 Since we're checking in there about a successful response, 1:54 it needs to go into the onResponse method. 1:57 Fortunately, that method is called with a response parameter already. 2:01 So we do not even need to change our code. 2:05 So we'll cut our try catch and 2:07 put it here, Into our response. 2:11 Our code is updated to make Asynchronous calls. 2:19 So let's try it out. 2:22 And another error. 2:31 Let's go into Logcat and see what it says. 2:34 So here we get a java.lang security exception permission denied, 2:47 missing internet permission. 2:50 Yes, we forgot to get permission for 2:53 our app to use the internet, that is a common mistake. 2:55 And can easily be fixed with a single permission line in AndroidManifest.xml. 2:59 So let's close down our Logcat again, go to manifest, android manifest, 3:04 and up here towards the top do uses permission. 3:11 We want Android Permission. 3:18 Internet. 3:25 And closer tag. 3:28 Great. 3:30 And let's run our app again. 3:32 Nice, no errors, and our emulator shows the default Hello World. 3:37 Let's take another look and Logcat and see our data. 3:43 And there it is, awesome, we'll talk about this 3:46 format of data called JSON in a little bit. 3:51 If you're not getting the data displayed here, you may want to debug your code and 3:56 execute it line-by-line to see what is happening. 4:00 Maybe adding additional log statements inside the on response method would help. 4:02 One common mistake is to have extra spaces, missing slashes, or 4:07 forgetting a comma when we built the URL. 4:11 So let's go back into main activity, close that down, and 4:14 take a look herein our callback. 4:17 What exactly is happening in our code here? 4:20 The on queue method executes the call, but does so by putting it into a queue. 4:22 This queue is provided by OkHttp and 4:28 executes the calls Asynchronously in the order they are added to the queue. 4:32 Since this is the only order in the queue it gets executed right away. 4:38 At that point, the call is executing in the background and 4:43 we are able to continue to execute code on the main UI thread, like normal. 4:46 We don't have any other code, but we could add another log statement down here. 4:51 Let's go ahead and do that to see the order of the log statements. 4:56 Log d tag, okay, and we'll do main UI code is running. 5:04 Hooray. 5:14 So, if this were Asynchronous code, it would process through log the response and 5:18 then log this new statement, but if we run our code now, And 5:23 look in Logcat, we see that our statement is displayed before the response. 5:30 This illustrates that the main UI thread 5:37 keeps working while the background thread is still working. 5:39 So what is a callback and how is it working? 5:43 Callbacks are very common in Java and Android development. 5:46 Asynchronous processing makes use of this, because it's the communication bridge 5:50 between a background worker thread and the main thread. 5:55 What's happening is this. 5:58 When we call the enqueue() method, 6:00 we're saying hey, call us back when you're done, here's how to do that. 6:01 We can imagine we are giving the OkHttp code a phone number to call us back on 6:06 when they're finished getting data from the web. 6:11 This callback is an object defined by the OkHttp Library. 6:13 OkHttp then says, 6:18 okay, as long as you give me a correct callback object, I'll use it. 6:20 When the call finishes in the background, 6:25 the onReponse method of this product is called. 6:28 Then the code we write inside it will be executed. 6:32 If there's an error the onFailure method would be called, 6:36 this could be an instant later or possibly a long time. 6:40 Generally, there's a limit on network connections, but 6:44 in theory, this background thread could run indefinitely. 6:47 As I mentioned, we'll talk about the JSON data in a little bit. 6:52 First though, let's make our code more robust by handling potential errors. 6:56
You need to sign up for Treehouse in order to download course files.Sign up