问题描述
我的 API 客户端
public class ApiClient {
public static final String BASE_URL = "http://baseurl.com/wp-json/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
我的端点接口
public interface ApiInterface {
@GET("posts/category/politics/recent-stories/")
Observable<ArrayList<ModelNewsPaged>> getPoliticsArrayListObservable(@Query("per_page") int perPage, @Query("page") int page);
@GET("posts/category/economy/recent-stories/")
Observable<ArrayList<ModelNewsPaged>> getEconomyArrayListObservable(@Query("per_page") int perPage, @Query("page") int page);
@GET("posts/category/sports/recent-stories/")
Observable<ArrayList<ModelNewsPaged>> getSportsArrayListObservable(@Query("per_page") int perPage, @Query("page") int page);
}
如何使用 flatMap 和 getSportsArrayListObservable 先让 Web 服务依次调用 getPoliticsArrayListObservable 和 getEconomyArrayListObservable 和 getSportsArrayListObservable不使用 lambda 函数?
How to make the web service call sequentially to getPoliticsArrayListObservable first and then getEconomyArrayListObservable and getSportsArrayListObservable using flatMap & without using lambda function?
我尝试过这样的事情:
final ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Observable.just(apiInterface)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Function<ApiInterface, Observable<ArrayList<ModelNewsPaged>>>() {
@Override
public Observable<ArrayList<ModelNewsPaged>> apply(ApiInterface apiInterface) throws Exception {
return apiInterface.getPoliticsArrayListObservable(10,1);
}
})
.subscribe(new Observer<ArrayList<ModelNewsPaged>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
Log.d("flatMap", "onComplete");
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
Log.d("flatMap", "onError");
}
@Override
public void onNext(ArrayList<ModelNewsPaged> integer) {
Log.d("flatMap", "Final result: " + integer.toString());
}
});
但这里的问题是,我无法链接更多的 flatMap 方法.任何帮助将不胜感激.
But the problem here is, I'm unable to chain any more flatMap methods. Any help will be much appreciated.
推荐答案
您可以使用 RxJava 的 Concat 运算符来顺序地进行 API 调用.如果您不关心顺序,您可以使用 Zip.
You can use Concat operator of RxJava to make the API call sequentially. If you don't care about the sequence you can use Zip.
嗯,我可能已经这样做了.如果您没有与 API 调用保持开放连接,则应将它们视为单一调用.
Well, I might have done it something like this.The API calls should be treated as Single if you are not keeping an open connection with them.
apiService.getDailyCounts()
.flatMap { apiService.updateCounts() }
.flatMap { apiService.changeTrialCourtCaseName() }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
这篇关于如何在Android RxJava中使用flatMap在不使用lambda函数的情况下依次进行三个Web服务调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!