Learn Pain Less

HomeOur TeamContact
Android
2 API requests serially using Retrofit2 and RxJava
Pawneshwer Gupta
Pawneshwer Gupta
May 07, 2018
1 min
2 API requests serially using Retrofit2 and RxJava

2 API requests serially using Retrofit2 and RxJava

If you want to call 2 or 3 api serially (one after another), then you can use RxJava with Retrofit2 and make this process simpler and easier. We can use flatMap() function of Rxjava’s Observable class, this function execute current Observable at same time, and then process them in serial. After processing all 3 Observables we can combines these 3 results and return them in callback.

here is example in Java:

This is simple chaining of requests.

List<String> result = new ArrayList<>();
Disposable disposable = retrofit.getApi_a()
.subscribeOn(Schedulers.io())
.flatMap((Function<ResponseType1, ObservableSource<ResponseType2>>) response1 -> {
result.add(response1.data);
return retrofit.getApi_b();
})
.flatMap((Function<ResponseType2, ObservableSource<ResponseType3>>) response2 -> {
result.add(response2.data);
return retrofit.getApi_c();
})
.map(response3 -> {
result.add(response3.data);
return response3;
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<Response3>() {
@Override
public void onNext(Response3 response3) {
Log.d(TAG, "result variable will have all the data");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.getMessage());
}
@Override
public void onComplete() {
}
});

And you can see we added all result in ArrayList and we got List of result in callback function. So the onNext() function will call at the end of last API call.

here is example in Kotlin:

  1. first of all get observable from retrofit interface class.
val result = new ArrayList<>()
val disposable = retrofit.getApi_a()
.subscribeOn(Schedulers.io())
.flatMap {
result.add(response1.data)
retrofit.getApi_b()
}
.flatMap {
result.add(response2.data)
retrofit.getApi_c()
}
.map {
result.add(response3.data)
response3
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
Log.d(TAG, "**result** is the list with all the data")
}, {
Log.e(TAG, e.getMessage())
})

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

androidretrofit2featured

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to use Text To Speech inside RecyclerView in Android
How to use Text To Speech inside RecyclerView in Android
July 27, 2020
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media