提问者:小点点

如何使用Retrofit 2和RxJava处理分页


我知道如何处理Retrofit响应,但我在使用rxjava处理RESTAPI分页时遇到了问题。

背景

我正在使用的rest api提供了以下响应,其中包含指向标题中下一页的链接:

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Link: <http://some-endpoint.com/pictures?page=2>; rel="next"
Vary: Accept
X-Total-Count: 80

[
    {
        "id": 3,
        "url": "",
        "picture": {
            "id": 6,
            "url": "",
            "name": "Chrysler"
        },
        "location": {
            "type": "Point",
            "coordinates": [
                10.108956,
                41.389576000000005
            ]
        }
    },
    {
        "id": 4,
        "url": "",
        "picture": {
            "id": 26,
            "url": "",
            "name": "Douglas Aircraft"
        },
        "location": {
            "type": "Point",
            "coordinates": [
                10.1977759999999997,
                41.426645
            ]
        }
    },
...
}

正确知道我设法获得分页请求到第2页与以下代码,但我不知道如何使这些分页请求,直到没有"下一个"头链接的响应,所以我可以保存所有的数据从所有的请求到数据库。

private Observable<List<PictureEntity>> getPictures(final LocationEntity locationEntity) {
    if (getDataFromServer()) {
        return mApiService.getPictures()
                .concatMap(new Func1<Response<List<Picture>>, Observable<Response<List<Picture>>>>() {
                    @Override
                    public Observable<Response<List<Picture>>> call(Response<List<Picture>> listResponse) {
                        String link = NetworkUtil.getNextLinkPaginate(listResponse.headers().get("Link"));
                        Timber.i(link);
                        return Observable.just(listResponse)
                                .concatWith(mApiService.getPicturesPaginate(link))
                                .reduce(new Func2<Response<List<Picture>>, Response<List<Picture>>,
                                        Response<List<Picture>>>() {
                                    @Override
                                    public Response<List<Picture>> call(Response<List<Picture>> listResponse,
                                                                            Response<List<Picture>> listResponse2) {
                                        listResponse.body().addAll(listResponse2.body());
                                        return listResponse;
                                    }
                                });
                    }
                })
                .concatMap(new Func1<Response<List<Picture>>, Observable<List<PictureEntity>>>() {
                    @Override
                    public Observable<List<PictureEntity>> call(Response<List<Picture>> listResponse) {
                        List<PictureEntity> pictureEntities =
                                mPictureDataMapper.transformToEntity(listResponse.body());
                        mDatabaseHelper.setPictureEntity(pictureEntities).subscribe();
                        return mDatabaseHelper.getNearbyPicturesEntity(locationEntity);
                    }
                });
    } else {
        return mDatabaseHelper.getNearbyPicturesEntity(locationEntity);
    }
}

对如何处理这种请求有什么建议吗?也许是. tokIt运算符?


共1个答案

匿名用户

您可以使用带有一点递归的concatMap。

Observable<List<PointOfSaleEntity>> getPointsOfSalePaginateUntilEnd(String link) {
  if (link.equals("")) {
      return Observable.empty();
  } else {      
      return mApiService.getPointsOfSalesPaginate(link)                     
                        .concatMap(listResponse -> Observable.concat(Observable.just(listResponse),mApiService.getPointsOfSalesPaginate(NetworkUtil.getNextLinkPaginate(listResponse.headers().get("Link")))));
  }
}

Observable<List<PointOfSaleEntity>> getPointsOfSalePaginateUntilEnd() {   
       return mApiService.mApiService.getPointsOfSales()                        
                         .concatMap(listResponse -> Observable.concat(Observable.just(listResponse),mApiService.getPointsOfSalesPaginate(NetworkUtil.getNextLinkPaginate(listResponse.headers().get("Link")))));
}