I'm using a REST API to query for a list of Person objects. Max limit is 100 people in response. I need to fetch all people, and the total amount is unknown. There is a field in the first response called "next", containing url for the next page. I need to chain these calls using RxJava/RxAndroid and Retrofit until the last response has an empty "next" field. Since the "next" field contains a pagination url, all subsequent calls will have different url from the first one. What is the most convenient way to do this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Something similar to this would work (a bit generalized):
public Observable<Response> paginate(String initialUrl){
AtomicReference<String> url = new AtomicReference<>(initialUrl)
return Observable.defer(() -> api.loadUsers(url.get())
.doOnNext(response -> url.set(response.next))
.repeatWhen(r -> r.takeWhile(!url.get().isEmpty()));
}
回答2:
You can do something like this.
Observable.just("input as one item if any").
.map(new Function<String, List<Person>>(){
@Override
public List<Person> apply(String inPut) throws Exception {
// using inPut, get url, service name, and other input
// params
String nextUrl = "firsturl";
List<Person> persons = new ArrayList<Persons>();
while(nextUrl != null){
//call service using plain retrofit passing nextUrl and get
//person objects
//add 100 person objects from each call
persons.add();
//get next Url
if(nextUrlFromResponse != null){
nextUrl = "next url from previous call";
}else{
nextUrl = null;
}
}
return persons;
}
}).subscribeOn(Schedulers.io()).observeOn(Androidmainthread);