使用rxjava + retrofit将参数传递给链式响应

时间:2015-03-17 12:11:59

标签: android retrofit rx-java

我有一个包含id,name和url的对象列表,我需要调用service来获取该列表中每个元素的图像url,但我还需要将该url添加到每个对象中,类似这样的

public class PhotoDomainEntity {
String id;
String name;
String urL;}

ID和名称已知,但我需要调用休息服务来获取此类网址

 Observable.from(entities)
                    .flatMap(new Func1<PhotoDomainEntity, Observable<PhotoEntity>>() {
                        @Override
                        public Observable<PhotoEntity> call(PhotoDomainEntity s) {
                            //call get photo details for each id

                            return PhotosApi.photosApi.getPhotoData(Constants.API_KEY, s.id,
                                    Constants.FLICKR_FORMAT, Constants.NO_JSONP_RESPONSE);
                        }
                    })

对于每个调用,我需要将url添加到PhotoDomainEntity。我如何传递id,并在响应每次通话时命名?

1 个答案:

答案 0 :(得分:2)

Observable.from(entities)
.flatMap(new Func1<PhotoDomainEntity, Observable<PhotoEntity>>() {

    @Override
    public Observable<PhotoEntity> call(final PhotoDomainEntity s) {
        //call get photo details for each id

        return PhotosApi.photosApi.getPhotoData(
            Constants.API_KEY,
            s.id,
            Constants.FLICKR_FORMAT,
            Constants.NO_JSONP_RESPONSE)
        .map(new Func1<PhotoEntity, PhotoEntity>() {

            @Override
            public PhotoEntity call(PhotoEntity fromServer) {
                // if mutable:
                fromServer.setId(s.getId());

                return fromServer;
            }
        });
    }
})