猿问

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

大家好,大家好!


我有两个独立的 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

    );

我的视图模型:


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();

}



眼眸繁星
浏览 154回答 2
2回答

手掌心

你可以做这样的事情,Observable<RootiTune> ituneObservable = serviceItune&nbsp; &nbsp; &nbsp; &nbsp; .getItuneArtistNameRx2NoList(ServiceItune.API_ITUNE_BASE_FULL_URL, query);Observable<RootLastFm> lastFmObservable = serviceLastFm&nbsp; &nbsp; &nbsp; &nbsp; .searchArtistRx(ServiceLastFm.API_LAST_FM_FULL_URL, query, ServiceLastFm.KEY_LAST_FM);Observable&nbsp; &nbsp; .zip(&nbsp; &nbsp; &nbsp; &nbsp; ituneObservable,&nbsp; &nbsp; &nbsp; &nbsp; lastFmObservable,&nbsp; &nbsp; &nbsp; &nbsp; (BiFunction<RootiTune, RootLastFm, Pair<RootiTune, RootLastFm>>) (ituneResult, lastFmResult) -> Pair&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.create(ituneResult, lastFmResult)&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; .toList()&nbsp; &nbsp; &nbsp; &nbsp; .subscribeOn(Schedulers.io())&nbsp; &nbsp; &nbsp; &nbsp; .observeOn(AndroidSchedulers.mainThread())&nbsp; &nbsp; &nbsp; &nbsp; .subscribe(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list -> listOfPair,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO handle error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; );获得 Pair 列表后,遍历列表并执行pair.first to get RootiTunepair.second to get RootLastFm

慕尼黑5688855

如果你不具备基本的接口RootiTune和RootLastFm,你可以使用Observable#zip()。首先,为两个结果创建包装器:class ResultWrapper(RootiTune ituneResult, RootLastFm lastFmResult) {&nbsp; &nbsp; public final RootiTune ituneResult;&nbsp; &nbsp; public final RootLastFm lastFmResult;&nbsp; &nbsp; public ResultWrapper(RootiTune ituneResult, RootLastFm lastFmResult) {&nbsp; &nbsp; &nbsp; &nbsp; this.ituneResult = ituneResult;&nbsp; &nbsp; &nbsp; &nbsp; this.lastFmResult = lastFmResult;&nbsp; &nbsp; }}之后,您可以压缩源: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))&nbsp; &nbsp; .subscribeOn(Schedulers.io())&nbsp; &nbsp; .observeOn(AndroidSchedulers.mainThread())&nbsp; &nbsp; .subscribe(resultWrapper -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseFmLiveData.setValue(resultWrapper.lastFmResult);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responsItuneeLiveData.setValue(resultWrapper.ituneResult);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; error -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO handle error&nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答