android rxjava2/retrofit2 chaining calls with pagination token

瘦欲@ 提交于 2020-01-05 04:54:30

问题


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);


来源:https://stackoverflow.com/questions/44509728/android-rxjava2-retrofit2-chaining-calls-with-pagination-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!