RetrofitAndAxJavaAndMVP二次封装

来源:互联网 发布:java引用xml文件 编辑:程序博客网 时间:2024/06/05 14:12

M层:

接口:

public interface MCallback {    void getMdata(String baseurl,String url);}
M层东西:

public class NewModle implements MCallback{    private NewPresenter newPresenter;  //P层拿过来    public NewModle(NewPresenter newPresenter){   //M层的有参构造,参数是P层,和P层绑定        this.newPresenter = newPresenter;    }    @Override    public void getMdata(String baseurl, String url) {    //实现自己的接口方法,P层调用
//请求网络,传来类型        Flowable<DBean> dBeanFlowable = RetrofitUtil.getInstence(baseurl).getApiService().get(url);        newPresenter.getDt(dBeanFlowable);   //调用P层的P与V的传数据方法,最终发送数据    }}

P层:

接口:

public interface PCallback {    void onPdata(String baseurl,String url);}
P层东西:

public class NewPresenter implements PCallback{    private DisposableSubscriber subscriber;  //这个变量是调用返回的Flowable有返回值,来进行一个防止内存泄漏    private ICallback iCallback;    public void attachView(ICallback iCallback){   //和V层同步接口        this.iCallback = iCallback;    }    public void detach(){   //防止内存泄漏的方法        if(iCallback!=null){            iCallback = null;   //V层接口        }        if(subscriber!=null){   //M层返来的Flowable            if(!subscriber.isDisposed()){                subscriber.dispose();            }        }    }    @Override    public void onPdata(String baseurl, String url) {   //V层只调用这个方法来启动请求数据        MCallback mc = new NewModle(this);      //多态的方式,调来M层,M层接口类型        mc.getMdata(baseurl,url);//调用M层实现M层接口的方法    }    public void getDt(Flowable<DBean> flowable){
//转型(DisposableSubscriber)
       subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribeWith(new DisposableSubscriber<DBean>() {   //new的DisposableSubscriber                    @Override                    public void onNext(DBean dBean) {                        iCallback.onSuccess(dBean);  //传到V层的接口,传送请求完的数据                    }                    @Override                    public void onError(Throwable t) {                    }                    @Override                    public void onComplete() {                    }                });    }}

V层:

接口:

public interface ICallback {    void onSuccess(Object object);}
V层东西:

V层就没啥了,就是加载视图:

V层调用P层attach方法绑定V层的接口,

V层调用P层实现P层的接口方法启动请求数据

就是V层的onDestroy方法中来调用P层的detach方法,来防止内存泄漏OM,


工具类:

public class RetrofitUtil {    private ApiService apService;    private static volatile RetrofitUtil retrofitUtil;    public RetrofitUtil(String baseurl){        if(apService==null){            OkHttpClient build = new OkHttpClient.Builder().build();            Retrofit builder = new Retrofit.Builder()                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                    .addConverterFactory(GsonConverterFactory.create())                    .client(build)                    .baseUrl(baseurl)                    .build();            apService = builder.create(ApiService.class);        }    }    public static RetrofitUtil getInstence(String baseurl){        if(null==retrofitUtil){            synchronized (RetrofitUtil.class){                if(retrofitUtil==null){                    retrofitUtil = new RetrofitUtil(baseurl);                }            }        }        return retrofitUtil;    }    public ApiService getApiService(){        return apService;    }}



boss's依赖:

    compile 'io.reactivex.rxjava2:rxjava:2.1.7'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'


ApiService:

public interface ApiServicee {    @GET    Flowable<NineBean> get(@Url String url);}
public interface ApiService {    @GET("93app/data.do")    Flowable<MessageBean<List<News>>> getNews(@QueryMap Map<String, String> map);}
路径:

nps.onPdata("http://120.27.23.105/","product/getCatagory");
baseurl中只放协议和域名,剩下的都在url中

原创粉丝点击