使用 RxJava、Retrofit、RxKotlin flatmap 按顺序调用多个 API

Calling multiple API in sequence using RxJava,Retrofit,RxKotlin flatmap

我需要调用 3 个不同的 api ...每个 api 按顺序接收另一个 api 输出的输入..

例如:API1 -> 输出 -> 将为 API2 输入 API2 -> 输出 -> 将为 API3

输入

在我的例子中,微调器包含 API1 ....关于微调器选择,我需要调用 API2 等等 目前我正在为每个 API 编写一个单独的代码并使用 Observer 调用它们...但是我想使用 RxJava、RxKotlin 和 Retrofit flatmap concepts.So 按顺序调用 APIs 有没有使用它的方式,我可以按顺序调用这三个 API,而无需单独编写每个

you can follow below link for chaining of API calling using flatMap

如果使用 Kotlin,则遵循 Chaining Multiple Retrofit Call Using RxJava/ Kotlin

从 Surinder 分享的链接中添加了片段

public Single<List<Restaurant>> getRestaurants(int userId) {
  return ddApi.getUserInfo(userId).flapMap(user -> {
  return ddApi.getAvailableRestaurants(user.defaultAddress.lat, 
              user.defaultAddress.lng);
 });
}



public class RestaurantFragment {

private CompositeDisposables disposables = new CompositeDisposables();
private RestaurantDataSource restaurantDataSource;

@Override
public void onResume() {
// subscribe to the Single returned by RestaurantApi
restaurantDataSource
  .getRestaurants(userId)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(new SingleObserver<Restaurant>() {
        @Override
        public void onSubscribe(Disposable d) {
            disposables.add(d);
        }

        @Override
        public void onSuccess(List<Restaurant> restaurants) {
            // update the adapter with restaurants
        }

        @Override
        public void onError(Throwable e) {
            // display an error message
        }
    });
}

 @Override
 public void onPause() {
   disposables.clear();
 }
}