具有Retrofit2和rxjava3的多个调用

问题描述 投票:0回答:2

我想使用翻新功能同时拨打多个电话。当两个电话都结束时,我想对结果有所帮助。

这是我的界面

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
2个回答
0
投票

您必须使用zip运算符

val service:IService

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

0
投票

Observable.combineLatest()是您所需要的,因此它将看起来像:

Observable<YourResult> observable = Observable
    .combineLatest( 
        api.getGlobal(), 
        api.getCountries(), 
        { global, country -> YourResult(global, country) }
    )
© www.soinside.com 2019 - 2024. All rights reserved.