嵌套请求示例多个请求组合结果
假设我们有一个 API,它允许我们在单个请求(getAllPets
)中获取对象元数据,以及其他具有单个资源(getSinglePet
)的完整数据的请求。我们如何在一个链中查询所有这些?
public class PetsFetcher {
static class PetRepository {
List<Integer> ids;
}
static class Pet {
int id;
String name;
int weight;
int height;
}
interface PetApi {
@GET("pets") Observable<PetRepository> getAllPets();
@GET("pet/{id}") Observable<Pet> getSinglePet(@Path("id") int id);
}
PetApi petApi;
Disposable petsDisposable;
public void requestAllPets() {
petApi.getAllPets()
.doOnSubscribe(new Consumer<Disposable>() {
@Override public void accept(Disposable disposable) throws Exception {
petsDisposable = disposable;
}
})
.flatMap(new Function<PetRepository, ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> apply(PetRepository petRepository) throws Exception {
List<Integer> petIds = petRepository.ids;
return Observable.fromIterable(petIds);
}
})
.flatMap(new Function<Integer, ObservableSource<Pet>>() {
@Override public ObservableSource<Pet> apply(Integer id) throws Exception {
return petApi.getSinglePet(id);
}
})
.toList()
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Pet>>() {
@Override public void accept(List<Pet> pets) throws Exception {
//use your pets here
}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {
//show user something goes wrong
}
});
}
void cancelRequests(){
if (petsDisposable!=null){
petsDisposable.dispose();
petsDisposable = null;
}
}
}