Im using retrofit (1.9) and trying to work out how i can call 2 apis and merge the results.
I guess the results should be merged when last api has finished returning a response...
How can i do this?
Udpate, as per Antons answer I have tried the following:
added to gradle:
compile 'io.reactivex:rxjava:1.0.+'
compile 'io.reactivex:rxandroid:0.23.+'
Api interface
@GET("/maps/api/place/details/json")
Observable<PlaceDetailResult1> getPlacesFrom1( @Query("key") String key, @Query("placeid") String placeid);
@GET("api/places/{id}/ratings")
Observable<PlaceDetailResult2> getPlacesFrom2(@Path("id") String id);
In fragment
IPlaces api = adapter.create(IPlaces.class); //endpoint1
IPlaces api2 = adapter2.create(IPlaces.class); //endpoint2
Observable.combineLatest(
api.getPlacesFrom1("key", placeId),
api2.getPlacesFrom2(placeId),
new Func2<PlaceDetailResult1, PlaceDetailResult2, MergedReviews>() {
@Override
public MergedReviews call(PlaceDetailResult placeDetailResult1, PlaceDetailResult2 placeDetailResult2) {
// processToMerge( placeDetailResult1, placeDetailResult2)
return mr;
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MergedReviews>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(MergedReviews mr) {
SetAdapter(mr.reviews);
SetPhotosAdapter(mr.photos);
}
});
One thing maybe worth noting, when I added RxJava to gradle i was getting some error, which went away after I did multiDexEnabled true
in the gradle file