合并和过滤来自独立来源的结果(RX_observables)

时间:2018-11-05 09:15:18

标签: java android retrofit2 rx-java2 android-mvvm

大家好,大家好!

我有两个独立的api(lastFM和iTune)源。代码分别为ich工作,我可以搜索(过滤)所需的艺术家。现在,我想结合使用此RXJava方法的结果,并在RecyclerView中显示它们。

我正在阅读有关Observable.zip或简单合并的信息 (顺序并不重要),但是我在这个问题上停留的时间太长了,我不知道该如何开始..是否应该为合并创建下一个模型:

public class iTuneAndLastFM {

    public JsonArray iTuneObject;
    public JsonArray LastFmObject;

    public iTuneAndLastFM(JsonArray iTuneObject, JsonArray lastFmObject) {
        this.iTuneObject = iTuneObject;
        LastFmObject = lastFmObject;
    }
}

还是没有必要?我卡在这里,我不能动..  所以请帮忙!

我当前的工作(情况):

public interface ServiceItune

String API_ITUNE_BASE_FULL_URL = "https://itunes.apple.com/search";
        @GET
        Observable<RootiTune> getItuneArtistNameRx2NoList(
                @Url String url,
                @Query("term") String artisName);

public interface ServiceLastFm

String API_LAST_FM_FULL_URL = "http://ws.audioscrobbler.com/2.0/?method=artist.search&format=json";

    Observable<RootLastFm> searchArtistRx(
            @Url String url,
            @Query("artist") String artistName,
            @Query("api_key") String key
    );

我的ViewModel:

public void getLastFmRx(String query) {
    disposables.add(serviceLastFm.searchArtistRx(ServiceLastFm.API_LAST_FM_FULL_URL, query,ServiceLastFm.KEY_LAST_FM)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(rootLastFmList -> responseLiveData.setValue(rootLastFmList))
            );
};
public  void getItubnRxTest2 (String query){
    disposables.add(serviceItune.getItuneArtistNameRx2NoList(ServiceItune.API_ITUNE_BASE_FULL_URL,query)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(rootiTuneList->responsItuneeLiveData.setValue(rootiTuneList) )
    );
};

最后,我的 iTunes模型

@AutoValue
public abstract class  RootiTune {

    public static TypeAdapter<RootiTune> typeAdapter(Gson gson){
        return new AutoValue_RootiTune.GsonTypeAdapter(gson);
    }
    @Nullable
    @SerializedName("resultCount")
    public abstract Integer getListSize();

    @Nullable
    @SerializedName("results")
    public abstract List<ResultiTune> getListSongs();

    @Nullable
    @SerializedName("errorMessage")
    public abstract String getErrorMessage();

}

LastFM模型

public class RootLastFm {
    @Expose
    private Results results;
    public List<ArtistLastFm> getListOfLastFm;
    public Results getResults() {
        return results;
    }
    public void setResults(Results results) {
        this.results = results;
    }
}

2 个答案:

答案 0 :(得分:0)

如果您没有RootiTuneRootLastFm的基本界面,则可以使用Observable#zip()。首先,为两个结果创建包装器:

class ResultWrapper(RootiTune ituneResult, RootLastFm lastFmResult) {
    public final RootiTune ituneResult;
    public final RootLastFm lastFmResult;

    public ResultWrapper(RootiTune ituneResult, RootLastFm lastFmResult) {
        this.ituneResult = ituneResult;
        this.lastFmResult = lastFmResult;
    }
}

之后,您可以压缩源:

Observable<RootiTune> ituneObservable = serviceItune.getItuneArtistNameRx2NoList(ServiceItune.API_ITUNE_BASE_FULL_URL,query);
Observable<RootLastFm> lastFmObservable = serviceLastFm.searchArtistRx(ServiceLastFm.API_LAST_FM_FULL_URL, query,ServiceLastFm.KEY_LAST_FM);
Observable.zip(ituneObservable, lastFmObservable, (ituneResult, lastFmResult) -> new ResultWrapper(ituneResult, lastFmResult))
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(resultWrapper -> {
            responseFmLiveData.setValue(resultWrapper.lastFmResult);
            responsItuneeLiveData.setValue(resultWrapper.ituneResult);
        }, 
        error -> {
            // TODO handle error
        });

答案 1 :(得分:0)

您可以这样做,

Observable<RootiTune> ituneObservable = serviceItune
        .getItuneArtistNameRx2NoList(ServiceItune.API_ITUNE_BASE_FULL_URL, query);
Observable<RootLastFm> lastFmObservable = serviceLastFm
        .searchArtistRx(ServiceLastFm.API_LAST_FM_FULL_URL, query, ServiceLastFm.KEY_LAST_FM);
Observable
    .zip(
        ituneObservable,
        lastFmObservable,
        (BiFunction<RootiTune, RootLastFm, Pair<RootiTune, RootLastFm>>) (ituneResult, lastFmResult) -> Pair
             .create(ituneResult, lastFmResult)
        )
        .toList()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            list -> listOfPair,
            error -> {
                // TODO handle error
            }
        );

获得配对列表后,遍历列表并执行

pair.first to get RootiTune
pair.second to get RootLastFm