In this video you'll learn how Retrofit supports synchronous and asynchronous requests out of the box.
Tip: turn on subtitles to deal with my accent. A shortened transcript is available below.
Find the tutorial for an easy read here:
►https://futurestud.io/tutorials/retro...
Watch 20+ Retrofit videos in our playlist here:
► • Android Retrofit Tutorial Series
----------------------------------------
Our book on Retrofit is available on leanpub:
►https://leanpub.com/retrofit-love-wor...
----------------------------------------
Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 320+ written tutorials and videos. We’re on a mission to provide new in-depth content every week.
Checkout 320+ technical in-depth tutorials:
►https://futurestud.io
Subscribe for two new videos every week:
►https://www.youtube.com/c/FutureStudi...
----------------------------------------
Follow us on social media to get updates on new content:
► / futurestud_io
► / futurestudiouniversity
►https://plus.google.com/+FutureStudio...
----------------------------------------
Shortened transcript:
In this video, you will learn the difference between synchronous and asynchronous execution. These terms were already used in you describe computing processes. A synchronous process is waiting for each step to be completed before moving on with the next one. Your simple Java code works synchronously, since it's executing one line, statement after statement, and waits until the line of code is completed, before going to the next line. Asynchronously on the other hand is if the process continues to run other commands after it started the computation, and when the result of the computation is available, it'll handle it.
This idea also exists in the area of network requests. The application can either hold and not do anything until the network request came back with a result, which would be synchronous requests or asynchronous requests would continue to run the app and simply update them when the network request has succeeded. Luckily. Retrofit set supports both ways, which we will look at now. I'm reusing the app we've implemented in the previous video on Retrofit.
So let's look at that in practice. This is our main activity where we gather some user input and pass the data to the sendNetworkRequest() method. I mentioned earlier that the call object allows both execution methods: synchronous or asynchronous. Let's look at the synchronous one. We're going to comment out the asynchronous one. Now the enqueue() executes the request asynchronously. That's why we have to pass a callback. Execute() on the other hand simply returns the result like a regular Java function call. So we're going to use execute() and have the result right away. We have to wrap it in a try-catch in case something goes wrong and the I/O level. Now all the things that happen behind the scenes from creating request, opening a network connection, parsing the server response is all wrapped in this single line. Isn't that fantastic? So let's run this. Let's add some sample input. Alright our app crashes :( So let's look what happened. So here you can see that there's NetworkOnMainThread exception. So the crash is actually for good reason. If you run a synchronous network request on the UI thread, it will hold the entire UI until the request is done. Essentially you have freezes. This is incredibly annoying to the user!
This doesn't mean that you can't use synchronous execution on Android. You simply have to do it on the thread, which isn't a UI thread. So let me show you that. Now instead of doing it from the UI thread, we will pass it to a background service. In this case the simple intent service. In the intent service, we will simply add the request we just made and see if it works from here. We still have to start the background thread. So we will do that with this intent. Once our method arrives here, it will start the service, jump into here and then execute the method synchronously on the background thread. So let's see if you get a response here. Okay, I'm going to add some data. Even though it doesn't really matter since we had it hard-coded in the background thread. But as you can see the synchronous way of doing [the request] actually works on the background thread. So feel free to use it whenever you are on the background thread or maybe in a Java server environment, but stick to the asynchronous way of doing it as soon as you are in a UI thread.
Смотрите видео Retrofit Tutorial — Synchronous and Asynchronous Requests онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Future Studio 02 Март 2017, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 30,528 раз и оно понравилось 218 людям.