0

So the scenario is one where we need to make 2 retrofit calls,

 @GET("/")
Observable<Search> searchMovies(@Query("apikey") String apiKey, @Query("s") String searchKey);

@GET("/")
Observable<Details> getMovie(@Query("apikey") String apiKey, @Query("t") String searchKey);

the first one is to get a list and then for each item in that list we will make a new call in order to get further information about that movie.

So the first question I have is how can I chain these 2 calls together inside ond rxjava method ?

The second question that i have follows up from this one in that I want to chain these 2 calls together inside and Rxjava method but then return a new observable pojo object based upon a few fields from each of the response. So for example say that request 1 contains "Name" and then request 2 contains "plot". I want to compose what would be a list of MovieInformation pojo objects based on these 2 fields and wrap that into an observable.

1 Answers1

0

You should use flatMap operator to chain these two calls.

Normally getMovie() should take in parameteres the result of searchMovies(). I suppose that apiKey,searchKey are the result of getMovies(apikey,searchKey) :

yourretrofitservice.searchMovies(apikey,searchKey)

                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())

                //waiting for response of yourretrofitservice.searchMovies() 

                .flatMap(new Func1<Search, Observable<Details>>() {

                    @Override
                    public Observable<Details> call(Search search_result) {

                        return yourretrofitservice.searchMovies(apiKey, searchKey); //maybe searchKey = search_result (you should define how to set the result of the First Retrofit call in the second call

                    }

                })

                //waiting for the response  of yourretrofitservice.getMovie()

                .flatMap(new Func1<Search, Observable<YourPojoClass>>() {

                    @Override
                    public Observable<YourPojoClass> call(Search search_result) {
                        //do something with pojo_object 
                        return pojo_object; second call

                    }

                })


                .subscribe(new Action1<YourPojoClass>() {

                    @Override
                    public void call(YourPojoClass  pojo_object) {
                        //Your final action
                    }

                });

FlatMap operator : http://reactivex.io/documentation/operators/flatmap.html

Karim
  • 322
  • 1
  • 10