【问题标题】:Multiple calls with retrofit2 and rxjava3使用 retrofit2 和 rxjava3 进行多次调用
【发布时间】:2020-07-15 19:15:41
【问题描述】:

我想使用改造同时拨打多个电话。当两个电话都结束时,我想用结果做点什么。

这是我的界面

public interface IService {
    @GET("all")
    Observable<Global> getGlobal();

    @GET("countries")
    Observable<Country> getCountries();
}

这是我的不完整代码

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://coronavirus-19-api.herokuapp.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();

        IService api = retrofit.create(IService.class);

        List<Observable<?>> requests = new ArrayList<>();
        requests.add(api.getGlobal());
        requests.add(api.getCountries());

【问题讨论】:

    标签: android retrofit rx-java retrofit2 rx-java2


    【解决方案1】:

    您必须使用zip 运算符

    val service:IService
    
    Observable.zip(
        service.global,
        service.countries,
        BiFunction { global, countries -> 
            // combine result
        }
    )
    

    【讨论】:

      【解决方案2】:

      Observable.combineLatest() 是你需要的,所以它看起来像:

      Observable<YourResult> observable = Observable
          .combineLatest( 
              api.getGlobal(), 
              api.getCountries(), 
              { global, country -> YourResult(global, country) }
          )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-22
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        • 2013-01-28
        • 2017-05-27
        • 1970-01-01
        • 2015-08-15
        相关资源
        最近更新 更多