android rxjava2/retrofit2 使用分页令牌链接调用 [英] android rxjava2/retrofit2 chaining calls with pagination token
问题描述
我正在使用 REST API 来查询 Person 对象列表.最大限制为 100 人响应.我需要把所有的人都拉过来,总金额不详.第一个响应中有一个名为next"的字段,包含下一页的 url.我需要使用 RxJava/RxAndroid 和 Retrofit 链接这些调用,直到最后一个响应有一个空的下一个"字段.由于next"字段包含分页网址,因此所有后续调用都将具有与第一个不同的网址.这样做最方便的方法是什么?
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?
推荐答案
与此类似的事情会起作用(有点笼统):
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()));
}
这篇关于android rxjava2/retrofit2 使用分页令牌链接调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!