RxJava - 获取2个组合请求的结果数据列表

时间:2018-05-16 16:20:01

标签: java android retrofit2 rx-java2

我有这两项服务:

private Observable<UMapsResponse> requestToServiceGetMaps(int idVenue, String accessToken) {
    return mService.getUMaps(idVenue, accessToken);
}

 private Observable<UMap> requestToServiceGetMapImageUrl(int idVenue, int idMap, String accessToken) {
    return mService.getUMap(idVenue, idMap, accessToken);
}

所以我将这两项服务结合起来:

    requestToServiceGetMaps(idVenue, accessToken)
            .flatMap(uMapsResponse -> Observable.just(uMapsResponse.getIndoorMaps()))
            .flatMapIterable(indoorMap -> indoorMap)
            .flatMap(
                    indoorMap -> requestToServiceGetMapImageUrl(idVenue, indoorMap.getId(), accessToken),
                    (indoorMap, uMap) -> Log.i(TAG, "MapsItem: " + new MapsItem(indoorMap, uMap))
            ).toList()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<List<Integer>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onSuccess(List<Integer> integers) {

                }

                @Override
                public void onError(Throwable e) {

                }
            });

所以我有两个问题:

  1. 为什么我在onSuccess方法中有整数列表?
  2. 如何获取MapsItem(indoorMap,uMap)列表?

1 个答案:

答案 0 :(得分:2)

你得到一个Integer列表,因为你从观察者链的最后一步发出整数,因为那是Log.i()返回的。

            ...
            .flatMap(
                    indoorMap -> requestToServiceGetMapImageUrl(idVenue, indoorMap.getId(), accessToken),
                    (indoorMap, uMap) -> new MapsItem(indoorMap, uMap)
            )
            .doOnNext( v -> Log.i(TAG, "MapsItem: " + v) )
            .toList()
            ...

请注意,最终flatMap()的结果是您要查找的新MapsItem