Handle Paging with RxJava

You could model it recursively:

Observable<ApiResponse> getPageAndNext(int page) {
  return getResults(page)
      .concatMap(new Func1<ApiResponse, Observable<ApiResponse>>() {

        public Observable<ApiResponse> call(ApiResponse response) {
          // Terminal case.
          if (response.next == null) {
            return Observable.just(response);
          return Observable.just(response)


Then, to consume it,

    .concatMap(new Func1<ApiResponse, Observable<ResponseObject>>() {

        public Observable<ResponseObject> call(ApiResponse response) {
          return Observable.from(response.results);

    .subscribe(new Action1<ResponseObject>() { /** Do something with it */ });

That should get you a stream of ResponseObject that will arrive in order, and most likely arrive in page-size chunks.

Leave a Comment