![]() ![]() Asynchronous RequestsĪdditionally to synchronous calls, Retrofit supports asynchronous requests out of the box. The deserialized response body is available via the. execute() method on a call object will perform the synchronous request in Retrofit 2. Retrofit 1.9 TaskService taskService = ServiceGenerator.createService(TaskService.class) Retrofit 2 TaskService taskService = ServiceGenerator.createService(TaskService.class) The following code snippets illustrate the synchronous request execution with Retrofit and assume that you’re familiar with the ServiceGenerator class introduced in the tutorial on how to create a sustainable Android client. This behavior changed from Retrofit v1 to v2. Let’s move to the point where we execute the actual request. That means, you can still interact with the app itself while waiting for the response. Synchronous methods provide the ability to use the return value directly, because the operation blocks everything else during your network request.įor non-blocking UI, you have to handle the request execution in a separated thread by yourself. You’ll run into the `NetworkOnMainThreadException` error. Warning: synchronous requests trigger app crashes on Android 4.0 or newer. That means the UI blocks during request execution and no interaction is possible for this period. Synchronous methods are executed on the main thread. However, the interface definition for synchronous and asynchronous requests are the same within Retrofit 2. ![]() The actual synchronous or asynchronous request is executed differently using the desired method on a later created call object. Within Retrofit 2, every request is wrapped into a Call object. Retrofit 1.9 public interface TaskService getTasks() Retrofit 2 public interface TaskService getTasks() The example below expects a list of tasks as the response when executing the method getTasks. Synchronous requests with Retrofit 1.9 are declared by defining a return type. Users define the concrete execution by setting a return type (synchronous) or not (asynchronous) to service methods. Retrofit supports synchronous and asynchronous request execution. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |