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!
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();
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(
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.