android rxjava2/retrofit2 chaining calls with pagi

2019-08-06 14:06发布

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?

2条回答
We Are One
2楼-- · 2019-08-06 14:33

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);
查看更多
We Are One
3楼-- · 2019-08-06 14:44

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()));
}
查看更多
登录 后发表回答