Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit & OkHttp - Is it possible to send only one request at a time?

I am using Retrofit 2.4.0 to send requests to a server. But sometimes server blocking my request if it has a similar timestamp in milliseconds with another request. I need to send request one at a time:

  1. Request A is sent
  2. Request B waits until the response for Request A received
  3. Request A completes with success or error
  4. Request B is sent

Is it possible to create such queue with Retrofit and OkHttp libraries?

like image 654
Joe Rakhimov Avatar asked Aug 24 '18 05:08

Joe Rakhimov


People also ask

What does retrofit mean?

transitive verb. : to furnish (something, such as a computer, airplane, or building) with new or modified parts or equipment not available or considered necessary at the time of manufacture. : to install (new or modified parts or equipment) in something previously manufactured or constructed.

What is a retrofit item?

(of new or modified parts, equipment, etc.) to fit into or onto existing equipment. to replace existing parts, equipment, etc., with updated parts or systems. noun. something that has been retrofitted. an instance of updating, enlarging, etc., with new or modified equipment: A retrofit could save thousands of dollars.

What is retrofit and its benefits?

Retrofit is a type-safe HTTP networking library used for Android and Java.

What is call in retrofit?

Call is a method to request to the webserver/API to get data.(Based on my understanding) Follow this answer to receive notifications.


2 Answers

I decided to use Dispatcher's setMaxRequests method to send request one at a time:

Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(1);

OkHttpClient client = new OkHttpClient.Builder()
                .dispatcher(dispatcher)
                .build()
like image 143
Joe Rakhimov Avatar answered Nov 14 '22 23:11

Joe Rakhimov


For your requirement, you can simply use Android AsyncTank and onPostExecute(). You can call your request B after getting the response of request A.

I feel no need to use Retrofit or OkHttp libraries. These libraries are useful when you send multiple requests at the same time.

like image 34
Arnab Kundu Avatar answered Nov 15 '22 01:11

Arnab Kundu