Android MVP 从入门到放弃(三)

好累呀。
MVP 可以和 retrofit + rejava2 配合使用。更好的服务项目。

代码懒的上了。 直接贴项目地址。

mvp

算了,还是上个部分代码。

retrofit的统一配置

public class RetrofitHelper {

    private static final int TIMEOUT_READ = 20;
    private static final int TIMEOUT_CONNECTION = 10;
    private static final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor()
            .setLevel(HttpLoggingInterceptor.Level.BODY);
//    private static CacheInterceptor cacheInterceptor = new CacheInterceptor();
    private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            //SSL证书
//            .sslSocketFactory(TrustManager.getUnsafeOkHttpClient())
//            .hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
            //打印日志
            .addInterceptor(interceptor)
            //设置Cache拦截器
//            .addNetworkInterceptor(cacheInterceptor)
//            .addInterceptor(cacheInterceptor)
//            .cache(HttpCache.getCache())
            //time out
            .connectTimeout(TIMEOUT_CONNECTION, TimeUnit.SECONDS)
            .readTimeout(TIMEOUT_READ, TimeUnit.SECONDS)
            .writeTimeout(TIMEOUT_READ, TimeUnit.SECONDS)
            //失败重连
            .retryOnConnectionFailure(true)
            .build();
//
    public static final String URL="http://wallpaper.apc.360.cn/";
//
    public static  T createApi(Class clazz) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(clazz);
    }

}

picmodel

public class PicModel implements PicContract.PicModel {

    public static PicModel newInstance() {
        return PicHodler.sInstance;
    }

    public static class PicHodler{
        private static final PicModel sInstance = new PicModel();
    }


    @Override
    public Observable getLogin(String name, String pwd) {


        return RetrofitHelper.createApi(PicApi.class).getPicType()
//                .compose(RxHelper.rxSchedulerHelper())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Function() {
                    @Override
                    public PicBean apply(PicBean picBean) throws Exception {
                        return picBean;
                    }
                });
//        return null;
    }
}

picpresenter

public class PicPresenter extends PicContract.PicPresenter {

    public static PicPresenter newInstance() {

        return PicPresenHolder.sInstance;
    }

    public static class PicPresenHolder {
        public static final PicPresenter sInstance = new PicPresenter();
    }

    @Override
    public void Onclicklogin(String name, String pwd) {

        if (model != null | view != null) {
            assert model != null;
            rxManager.register(model.getLogin(name, pwd).subscribe(new Consumer() {
                @Override
                public void accept(PicBean picBean) throws Exception {
                    if (view == null) {
                        return;
                    }
                    view.onSuccess(picBean);
                }
            }, new Consumer() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    if (view == null)
                        return;

                    view.onError(throwable.toString());
                }
            }));

        }
    }

    @Override
    public PicContract.PicModel getModel() {
        return PicModel.newInstance();
    }
}

其他的下载项目看下。 希望对你们有些帮助。

好烦啊、 肚子好饿、早知道不做安卓开发了。%>_<%

你可能感兴趣的:(Android MVP 从入门到放弃(三))