0

我有一个函数,它返回一个包含来自 html 页面的列表的对象:

@Override
public Observable<MediaList> getMediaListFromUrl(String url) {
    return getHtml(url)
            .map(new Func1<String, MediaList>() {
                @Override
                public MediaList call(String s) {
                    return interpreter.interpretMediaResultsFromHtml(s);
                }
            });
}

接下来我想做的是:

for(Media media : mediaList.getMedias()) {
    if (media.getImageUrl().contains("couvertureAjax")) {
        // fetch media.getImageUrl
        // replace the image url with the result
    }
}

最后,返回带有替换图像 url 的原始 MediaList 对象。我想我应该在某个地方使用flatmapand from,但我不知道具体如何。

媒体列表.java

import android.os.Parcel;
import android.os.Parcelable;

import java.util.List;

public class MediaList implements Parcelable {
    public static final String TAG = MediaList.class.getSimpleName();
    private List<Media> medias;
    private String nextPageUrl;

    public MediaList() {}

    protected MediaList(Parcel in) {
        medias = in.createTypedArrayList(Media.CREATOR);
        nextPageUrl = in.readString();
    }

    public List<Media> getMedias() {
        return medias;
    }

    public void setMedias(List<Media> medias) {
        this.medias = medias;
    }

    public String getNextPageUrl() {
        return nextPageUrl;
    }

    public void setNextPageUrl(String nextPageUrl) {
        this.nextPageUrl = nextPageUrl;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(medias);
        dest.writeString(nextPageUrl);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<MediaList> CREATOR = new Creator<MediaList>() {
        @Override
        public MediaList createFromParcel(Parcel in) {
            return new MediaList(in);
        }

        @Override
        public MediaList[] newArray(int size) {
            return new MediaList[size];
        }
    };
}
4

2 回答 2

1

由于我没有这里涉及的所有类,所以我编写了一个 Rx 代码来帮助实现你想要的:

getHtml(url)
                .flatMap(new Func1<String, Media>() {
                    @Override
                    public Media call(String s) {
                        // Here update code interpreter.interpretMediaResultsFromHtml(s) and make sure your return List<Media> from here
                        return Observable.from(interpreter.interpretMediaResultsFromHtml(s));
                    }
                })
                .map(new Func1<Media, Media>() {
                    @Override
                    public Media call(Media media) {
                        // Write code here to update Url in media and return it
                        return null; // Return updated media object from here
                    }
                })
                .toList()
                .subscribe(new Action1<List<Media>>() {
                    @Override
                    public void call(List<Media> mediaList) {
                        // Here MediaLkist object will be your updated List<Media>
                    }
                });

让我知道它是否解决了您的问题或者您需要更多详细信息。您想通过 RxJava 实现的目标绝对有可能。

于 2016-04-09T11:13:52.143 回答
0

一种方法:

@Override
public Observable<MediaList> getMediaList(String url) {
    return getHtml(url)
            .flatMap(new Func1<String, Observable<MediaList>>() {
                @Override
                public Observable<MediaList> call(String s) {
                    MediaList mediaList = interpreter.interpretMediaResultsFromHtml(s);
                    Observable<List<Media>> list = Observable.from(mediaList.getMedias())
                            .flatMap(new Func1<Media, Observable<Media>>() {
                                @Override
                                public Observable<Media> call(Media media) {
                                    if (media.needImagePreload()) {
                                        return getMediaLoadedImage(media.getImageUrl(), media);
                                    } else {
                                        return Observable.just(media);
                                    }
                                }
                            })
                            .toList();
                    return Observable.zip(Observable.just(mediaList.getNextPageUrl()), list, new Func2<String, List<Media>, MediaList>() {
                        @Override
                        public MediaList call(String s, List<Media> medias) {
                            return zipMediaList(s, medias);
                        }
                    });
                }
            });
}


private MediaList zipMediaList(String s, List<Media> medias) {
    MediaList mediaList = DefaultFactory.MediaList.constructDefaultInstance();
    mediaList.setNextPageUrl(s);
    mediaList.setMedias(medias);
    return mediaList;
}

我认为它们是更简单的方法,但它确实有效。

于 2016-04-12T15:17:10.720 回答