mvp和rxjava继续学习

google 官方的mvp例子:html

https://github.com/googlesamples/android-architecture/tree/todo-mvp-dagger/java

国内集成了不少目前热门框架的项目:react

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/1125/6803.htmlandroid

android进阶篇 MVP+Retrofit+RxJava框架结合:git

http://m.2cto.com/kf/201608/538697.htmlgithub

响应式编程:RxJava/RxAndroid 使用实例实践:编程

http://www.jianshu.com/p/031745744bfaapp

rxjava使用场景:框架

http://blog.csdn.net/lzyzsd/article/details/50120801ide

RxJava 2.0 使用详解

http://blog.csdn.net/aiynmimi/article/details/53382567

Rxjava 官方github

https://github.com/ReactiveX/RxJava/

【腾讯大神】一步一步实现Android的MVP框架

http://dev.qq.com/topic/5799d7844bef22a823b3ad44

RxJava 合并组合两个(或多个)Observable数据源

http://blog.csdn.net/jdsjlzx/article/details/52415615

给 Android 开发者的 RxJava 详解

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/1012/3572.html#toc_17

 

retrofit2和rxjava2应用:

compile 'com.google.code.gson:gson:2.7'
compile( 'com.squareup.retrofit2:retrofit:2.2.0'){
    exclude group: 'com.squareup.okhttp3'
}
compile ('com.squareup.retrofit2:converter-gson:2.2.0'){
    exclude group : 'com.google.code.gson'
    exclude group: 'com.squareup.okhttp3'
}
compile ('com.squareup.retrofit2:adapter-rxjava2:2.2.0'){
    exclude group: 'io.reactivex'
    exclude group: 'com.squareup.okhttp3' }
compile 'io.reactivex.rxjava2:rxjava:2.0.8'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:latest.release'
compile 'com.squareup.okhttp3:okhttp:latest.release'
compile 'com.squareup.okio:okio:latest.release'

遇到一个问题,不知道flatmap和doOnNext怎么配合使用,问题见注释:

@Override
    public void start(final String agentId, String typeId) {
        mRootView.showProgress();

        LogUtils.e("main Thread:"+Thread.currentThread().getId());
        Disposable o = mModel.getSecondaryGoodsTypes(agentId, typeId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<BaseResponse<List<GoodsType>>>() {
                    @Override
                    public void accept(@NonNull BaseResponse<List<GoodsType>> listBaseResponse) throws Exception {
                        LogUtils.e("doOnNext current Thread:"+Thread.currentThread().getId());
                        List<GoodsType> goodsTypes = listBaseResponse.getData();
                        mRootView.updateTypeList(goodsTypes);
                    }
                }).doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        LogUtils.e("doOnError current Thread:"+Thread.currentThread().getId());
                        mRootView.hideProgress();
                        mRootView.showCommonErrorPage();
                    }
                })
                .observeOn(Schedulers.newThread())
                .flatMap(new Function<BaseResponse<List<GoodsType>>, Observable<BaseResponse<GoodsList>>>() {
                    @Override
                    public Observable<BaseResponse<GoodsList>> apply(@NonNull BaseResponse<List<GoodsType>> listBaseResponse) throws Exception {

                        LogUtils.e("flatMap apply current Thread:"+Thread.currentThread().getId());
                        //先经过一级分类id拿商品二级分类,再默认拿第一个分类下的商品列表
                        if (listBaseResponse != null) {
                            List<GoodsType> list = listBaseResponse.getData();
                            if (list != null && list.size() > 0) {
                                int secondTypeId = list.get(0).getId();
                                return mModel.getGoodsByType(agentId, String.valueOf(secondTypeId), 20, 0);
                            }
                        }
                        return Observable.just(null);
                    }
                })
                //.observeOn(AndroidSchedulers.mainThread())//加上这行就没有回调了,不加的话就是在子线程而不是在主线程
                .subscribeWith(new DisposableObserver<BaseResponse<GoodsList>>() {
                    @Override
                    public void onNext(BaseResponse<GoodsList> goodsListBaseResponse) {

                        LogUtils.e("subscribeWith onNext current Thread:"+Thread.currentThread().getId());
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e("subscribeWith onError current Thread:"+Thread.currentThread().getId());
                        mRootView.hideProgress();

                    }

                    @Override
                    public void onComplete() {
                        mRootView.hideProgress();
                    }
                });
相关文章
相关标签/搜索