开发者社区> 问答> 正文

RxJava ?observable 嵌套问题 报错

"

问题:
RxView.clicks 之后请求网络,使用retrofit,如下:

RxView.clicks(findViewById(R.id.button2)) .subscribe(new Consumer<Unit>() { @Override public void accept(Unit unit) throws Exception { Http.get().create(TimeApi.class).getLicense(TimeApi.url) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<TimeModel>() { @Override public void accept(TimeModel timeModel) throws Exception { btn.setText(timeModel.getData() + ""); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception {

                                }
                            });
                }
            });
            </code></pre> 

怎么消除嵌套?
我使用了concatMap、flatMap,compose,都有一些问题。比如Scheduler问题,或者click事件不点击直接执行问题:


        ObservableTransformer<Unit, TimeModel> transformer = new ObservableTransformer<Unit, TimeModel>() {

            @Override
            public ObservableSource<TimeModel> apply(Observable upstream) {
                return Http.get().create(TimeApi.class).getLicense(TimeApi.url);
            }
        };

        RxView
                .clicks(btn2)
                .compose(transformer)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TimeModel>() {
                    @Override
                    public void accept(TimeModel model) throws Exception {
                        Log.d("HHHH", model.getData()+"");
                        btn2.setText(String.valueOf(model.getData()));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d("HHHH", "error");
                        btn2.setText("---------------"+throwable.getMessage());
                    }
                });

        RxView.clicks(btn2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .concatMap(new Function<Unit, ObservableSource<TimeModel>>() {
                    @Override
                    public ObservableSource<TimeModel> apply(Unit unit) throws Exception {
                        return Http.get().create(TimeApi.class).getLicense(TimeApi.url);
                    }
                })
                .subscribe(new Consumer<TimeModel>() {
                    @Override
                    public void accept(TimeModel model) throws Exception {
                        Log.d("HHHH", model.getData()+"");
                        btn2.setText(String.valueOf(model.getData()));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d("HHHH", "error");
                        btn2.setText("---------------"+throwable.getMessage());
                    }
                });


        Disposable subscribe = RxView
                .clicks(btn2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new Function<Unit, ObservableSource<TimeModel>>() {
                    @Override
                    public ObservableSource<TimeModel> apply(Unit unit) throws Exception {
                        Log.d("HHHH", "request");
                        return Http.get().create(TimeApi.class).getLicense(TimeApi.url);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TimeModel>() {
                    @Override
                    public void accept(TimeModel model) throws Exception {
                        Log.d("HHHH", model.getData()+"");
                        btn2.setText(String.valueOf(model.getData()));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d("HHHH", "error");
                        btn2.setText("---------------"+throwable.getMessage());
                    }
                });
" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/232469af75a141efbb9d12637f3e560a.png)

展开
收起
因为相信,所以看见。 2020-05-26 13:57:18 703 0
1 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载