我想使用翻新功能同时拨打多个电话。当两个电话都结束时,我想对结果有所帮助。
这是我的界面
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());
您必须使用zip运算符
val service:IService
Observable.zip(
service.global,
service.countries,
BiFunction { global, countries ->
// combine result
}
)
Observable.combineLatest()
是您所需要的,因此它将看起来像:
Observable<YourResult> observable = Observable
.combineLatest(
api.getGlobal(),
api.getCountries(),
{ global, country -> YourResult(global, country) }
)