RxAndroid连锁动作

时间:2016-05-06 13:17:53

标签: java android retrofit rx-java rx-android

我正在使用RxAndroid + Retrofit,我想要做的是将两个动作组合在一起。例如,我有两种方法。首先在onNext块中返回一些URL。

apiRequests.getApi().getURL()
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<CreateUserResponse>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(CreateUserResponse createUserResponse) {
                    secondMethos(createUserResponse.getURL()); // HERE GOES THE URL

                }
            });

第二种方法是使用此URL执行其他操作

public void secondMethod(URL){
    Observable.create(new Observable.OnSubscribe<Object>() {


        @Override
        public void call(Subscriber<? super Object> subscriber) {

        }
    });

    Observable.create(new Observable.OnSubscribe<List<Arg>>() {
        @Override
        public void call(Subscriber<? super List<Arg>> subscriber) {
            subscriber.onNext(test(URL));
            subscriber.onCompleted();

        }
    }).subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<Arg>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(List<Arg> params) {

                }
            });


}

是否可以将这些方法合并为一个?感谢

1 个答案:

答案 0 :(得分:1)

您可以简单地映射这些值

    apiRequests.getApi().getURL()
            .map(new Func1<CreateUserResponse, List<Arg>>() {
                public List<Arg> call(CreateUserResponse response) {
                    return test(response.getURL());
                }
            })
            .subscribeOn(Schedulers.newThread())
            .observerOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<Arg>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(List<Arg> params) {

                }

请注意,如果您希望使用每个Arg而不是List<Arg>调用onNext,则可以使用flatMapInterable,如下所示。

    apiRequests.getApi().getURL()
            .flatMapIterable(new Func1<CreateUserResponse, List<Arg>>() {
                public List<Arg> call(CreateUserResponse response) {
                    return test(response.getURL());
                }
            })
            .subscribeOn(Schedulers.newThread())
            .observerOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Arg>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Arg params) {

                }
            });