美文网首页
Android中的RxJava,Retrofit,MVP的使用

Android中的RxJava,Retrofit,MVP的使用

作者: 子墨_guo | 来源:发表于2016-04-25 01:22 被阅读750次

    RxJava,Retrofit,MVP都是近几年讨论的比较热门的话题,我们也来凑个热闹!

    本文不展开讨论,分别讨论的文章有很多,后续有时间给出RxJava,Retrofit的源码分析

    先来说一下MVP(Model View Presenter)熟悉MVC结构的应该很容易理解MVP,这里先给出MVP的图解

    MVP图解
    • model具体业务逻辑处理的实现
    • view负责显示数据、提供友好界面跟用户交互
    • presenter负责完成View于Model间的交互

    下面给出一个使用这三者的demo,由于使用Retrofit和RxJava,数据获取的过程变得很简洁,此demo没有按照严格的MVP结构,遵从了数据和view分开处理的原则,降低了代码的耦合度,少定义几个接口

    View代码层

    public class MainActivity extends AppCompatActivity implements LoadingBannerView{ 
        @Bind(R.id.show_banner_string) 
        TextView showBanner; 
        BannerPresenter presenter; 
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.activity_main); 
            ButterKnife.bind(this); 
            presenter = new BannerPresenter(MainActivity.this);
            presenter.loadData(); 
        } 
         @Override 
        public void showData(List<BannerVO> bannerVOs) { 
                showBanner.setText(bannerVOs.get(i).getPicUrl()); 
        } 
        @Override 
        public void showNetworkErrorView() { } 
        @Override 
        public void hideNetworkErrorView() { } 
        @Override 
        public void showLoading() { } 
        @Override 
        public void hideLoading() { } 
        @Override 
        public void showEmptyView() { } 
        @Override 
        public void hideEmptyView() { } 
        @Override 
        public void showError(String message) { }
    }
    

    写的比较简单,根据需求私人私人定制

    view层的接口

    public interface LoadingBannerView extends LoadingDataView{
        void showData(List<BannerVO> bannerVOs);
    }
    
    public interface LoadingDataView { 
        void showNetworkErrorView(); 
        void hideNetworkErrorView(); 
        void showLoading(); 
        void hideLoading();
        void showEmptyView(); 
        void hideEmptyView(); 
        void showError(String message);
    }
    

    present层代码

    public class BannerPresenter implements CommonPresenter { 
        LoadingBannerView view; 
        public BannerPresenter(LoadingBannerView view) { 
            this.view = view; 
        } 
        @Override 
        public void loadData() { 
            HttpService.getInstance().getCommonApi().getBannerList("", "", "", "") 
        .lift(new OperatorRequestMap<List<BannerVO>>())
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(new Action1<List<BannerVO>>() { 
            @Override 
            public void call(List<BannerVO> bannerVOs) { 
                if (bannerVOs != null && bannerVOs.size() > 0) { 
                    view.showData(bannerVOs); 
                }else { 
                    view.showEmptyView(); 
                } 
            } 
        }, new Action1<Throwable>() { 
            @Override 
            public void call(Throwable throwable) { 
                if (throwable instanceof NetworkConnectionException) {
                    view.showNetworkErrorView(); 
                  } 
              } 
        }); 
      }
     }
    

    present层的接口

    public interface CommonPresenter { void loadData();}
    

    model层主要是实体,省略一百行代码...

    RxJava使用

    public class HttpService { 
         private CommonApi commonApi; 
         private HttpService() { 
            Retrofit.Builder builder = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create()) 
            .baseUrl(HttpContants.DEFAULT_HOST_URL); 
        OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder()
        .addNetworkInterceptor(new Interceptor() { 
            @Override 
            public Response intercept(Chain chain) throws IOException { 
                Request request = chain.request(); 
                Request newReq = request.newBuilder() .build(); 
                return chain.proceed(newReq); 
            } 
        }); 
        if (BuildConfig.DEBUG){ 
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
             loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
            okHttpBuilder.addInterceptor(loggingInterceptor); 
          } 
        OkHttpClient client = okHttpBuilder.build();
        builder.client(client); 
        commonApi = builder.build().create(CommonApi.class); 
       } 
        private static class HttpServiceHandler{ 
            public static final HttpService INSTANCE = new HttpService();
        } 
        public static HttpService getInstance(){ 
            return HttpServiceHandler.INSTANCE; 
        } 
        public static CommonApi getCommonApi() { 
            return HttpService.getInstance().commonApi; 
        }
    }
    

    retrofit的使用

    public interface CommonApi { 
        @FormUrlEncoded 
        @POST("") 
        Observable<BaseBean<List<BannerVO>>> getBannerList(@Field("") String method, @Field("") String bannerUpdateVer, @Field("") String sessionKey, @Field("") String bannerType);
    }
    

    欲知详情,请听下回解说

    Demo地址:https://github.com/zimoguo/MVPDemo

    相关文章

      网友评论

          本文标题:Android中的RxJava,Retrofit,MVP的使用

          本文链接:https://www.haomeiwen.com/subject/hvwfrttx.html