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
.
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>() {@Overridepublic void onNext(Response3 response3) {Log.d(TAG, "result variable will have all the data");}@Overridepublic void onError(Throwable e) {Log.e(TAG, e.getMessage());}@Overridepublic 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.
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())})
Quick Links
Legal Stuff
Social Media