サービスに対して2つのリクエストを作成し、結果を組み合わせる必要があります。
ServiceA()=> [{"id":1,"name":"title"},{"id":1,"name":"title"}]
ServiceB(id
)=> {"field":"value","field1":"value"}
現在、結果を結合することができましたが、id
をパラメーターとしてServiceBに渡し、最初の結果にアクセスする必要があります。
私がこれまでに試したこと:
Retrofit repo = new Retrofit.Builder()
.baseUrl("https://api.themoviedb.org/3/genre/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
Observable<GenerosResponse> Genres = repo
.create(services.class)
.getAllGeneros("movie","list","da0d692f7f62a1dc687580f79dc1e6a0")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<ResponseMovies> Movies = repo
.create(services.class)
.getAllMovies("28","movies","da0d692f7f62a1dc687580f79dc1e6a0",12)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
Observable<CollectionsMovies> combined = Observable.Zip(Genres, Movies, new Func2<GenerosResponse, ResponseMovies, CollectionsMovies>() {
@Override
public CollectionsMovies call(GenerosResponse generosResponse, ResponseMovies responseMovies) {
return new CollectionsMovies(generosResponse, responseMovies);
}
});
combined.
subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
編集
Solution@Maxim Ostrovidovの答えによると:
private Observable<GenerosResponse> makeRequestToServiceA() {
return service.getAllGeneros("movie","list","da0d692f7f62a1dc687580f79dc1e6a0"); //some network call
}
private Observable<ResponseMovies> makeRequestToServiceB(Genre genre) {
return service.getAllMovies(genre.getId(),"movies","da0d692f7f62a1dc687580f79dc1e6a0","created_at.asc"); //some network call based on response from ServiceA
}
void doTheJob() {
makeRequestToServiceA()
.flatMap(userResponse -> Observable.just(userResponse.getGenres())) //get list from response
.flatMapIterable(baseDatas -> baseDatas)
.flatMap(new Func1<Genre, Observable<? extends ResponseMovies>>() {
@Override
public Observable<? extends ResponseMovies> call(Genre genre) {
return makeRequestToServiceB(genre);
}
}, new Func2<Genre, ResponseMovies, CollectionsMovies>() {
@Override
public CollectionsMovies call(Genre genre, ResponseMovies responseMovies) {
return new CollectionsMovies(genre,responseMovies);
}
}).
subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(....);
}
私が理解したように-別のリクエストの結果に基づいてリクエストを作成し、両方の結果を組み合わせる必要があります。そのために、次のflatMap
演算子バリアントを使用できます: Observable.flatMap(Func1 collectionSelector、Func2 resultSelector)
指定された関数の結果を、ソースObservableと指定されたコレクションObservableによって発行された値のペアに発行するObservableを返します。
コードを書き換える方法を示す簡単な例:
private Observable<String> makeRequestToServiceA() {
return Observable.just("serviceA response"); //some network call
}
private Observable<String> makeRequestToServiceB(String serviceAResponse) {
return Observable.just("serviceB response"); //some network call based on response from ServiceA
}
private void doTheJob() {
makeRequestToServiceA()
.flatMap(new Func1<String, Observable<? extends String>>() {
@Override
public Observable<? extends String> call(String responseFromServiceA) {
//make second request based on response from ServiceA
return makeRequestToServiceB(responseFromServiceA);
}
}, new Func2<String, String, Observable<String>>() {
@Override
public Observable<String> call(String responseFromServiceA, String responseFromServiceB) {
//combine results
return Observable.just("here is combined result!");
}
})
//apply schedulers, subscribe etc
}
ラムダの使用:
private void doTheJob() {
makeRequestToServiceA()
.flatMap(responseFromServiceA -> makeRequestToServiceB(responseFromServiceA),
(responseFromServiceA, responseFromServiceB) -> Observable.just("here is combined result!"))
//...
}
探している演算子はflatMap()
です
serviceA.getAllGeneros("movie","list","da0d692f7f62a1dc687580f79dc1e6a0")
.flatMap(genres -> serviceB.getAllMovies(genres.getId() ......))