3

How to call multiple requests at the same time in Retrofit 2

I have 2 different api, and I want to call them at the same time. How can I do this ?

Thanks!

ARR.s
  • 769
  • 4
  • 20
  • 39

2 Answers2

1

You could use enqueue method of retrofit2 for asynchronously calling multiple request at the same time.

Here is the documentation for the enqueue:

/**
* Asynchronously send the request and notify {@code callback} of its response or if an error
* occurred talking to the server, creating the request, or processing the response.
*/
void enqueue(Callback callback);

Here is the pseudo code how you could do that:

Call<MyResponse> call = retroService.getSomeData();
call.enqueue(new Callback<MyResponse>() {
  @Override
  public void onResponse(
  public void onFailure(
});
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
  • This doesn't solve the problem. This is calling A REQUEST and waiting for the result. – Maher Nabil Feb 26 '18 at 12:20
  • No it doesnt wait for the Response, Purpose of enqueue is Asynchronous it doesnt wait for the result to come, next consecutive statements are executed. – parvez rafi Jan 01 '20 at 11:03
-1

Just create an observer that pass parameters of the two servers. Help the code below

OkHttpClient okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();

            Request.Builder builder = originalRequest.newBuilder().header("Authorization",
                    Credentials.basic("aUsername", "aPassword"));

            Request newRequest = builder.build();
            return chain.proceed(newRequest);
        }
    }).build();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com")
.client(okHttpClient)
.build();

Font

Pedro Simões
  • 309
  • 2
  • 11