How to use flatMap in Android RxJava to make three web service call sequentially without using lambda function?

烂漫一生 提交于 2020-05-14 18:26:07

问题


My API Client

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;
    }
}

My endpoint interface

    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);
    }

How to make the web service call sequentially to getPoliticsArrayListObservable first and then getEconomyArrayListObservable and getSportsArrayListObservable using flatMap & without using lambda function?

I tried something like this:

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());
                    }
                });

But the problem here is, I'm unable to chain any more flatMap methods. Any help will be much appreciated.


回答1:


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.

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()


来源:https://stackoverflow.com/questions/49717233/how-to-use-flatmap-in-android-rxjava-to-make-three-web-service-call-sequentially

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!