Android_Retrofit+RxJava+MVP+网络请求多次判断

//依赖

  compile 'io.reactivex.rxjava2:rxjava:2.1.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.squareup.retrofit2:retrofit:2.0.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'//解析使用
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'//与RxJava结合时使用

//Retrofit
//ApiService

public interface ApiService {
    //http://120.27.23.105/product/getProductCatagory?cid=1
    @GET("product/getProductCatagory")
    Flowable<Bean> get(@QueryMap Map<String,String> map);

    @GET("product/getProductCatagory")
    Flowable<Bean> get2(@QueryMap Map<String,String> map);

}

//RetrofitUtils

public class RetrofitUtils {
    private static volatile RetrofitUtils instance;
    private final Retrofit retrofit;

    private RetrofitUtils(String baseurl) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseurl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static RetrofitUtils getInstance(String baseurl) {
        if (instance == null) {
            synchronized (RetrofitUtils.class) {
                if (instance == null) {
                    instance = new RetrofitUtils(baseurl);
                }
            }
        }
        return instance;
    }

    public Retrofit getretrofit(){
        return retrofit;
    }

}

//presenter
//BasePresenter

public interface BasePresenter {
    void get(String baseurl,Map<String,String> map, int tag);
}

//Presenter

public class Presenter implements BasePresenter{

    private IView iv;
    private DisposableSubscriber subscriber;

    public Presenter(IView iv) {
        this.iv = iv;
    }

    @Override
    public void get(String baseurl,Map<String, String> map,int tag) {
        Model model = new Model(this);
        model.get(baseurl,map,tag);
    }

    public void getData(Flowable flowable,final int tag){
        subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        iv.onSuccess(o,tag);
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed((Exception) t);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }



    public void getData1(Flowable flowable,final int tag){
        subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        iv.onSuccess(o,tag);
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed((Exception) t);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }






    //防止内存泄漏
    public void detatch(){
        if (iv != null) {
            iv = null;
        }
        if(subscriber!=null){
            if(!subscriber.isDisposed()){
                subscriber.dispose();
            }
        }
    }

}

//model
//IModel

public interface IModel {
    void get(String baseurl,Map<String,String> map,int tag);
}

//Model

public class Model implements IModel{

    private Presenter presenter;

    public Model(Presenter presenter) {
        this.presenter = presenter;
    }



    @Override
    public void get(String baseurl, Map<String, String> map, int tag) {

        if(tag==1)
        {

            Flowable<Bean> flowable = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class).get(map);
            presenter.getData(flowable,tag);

        }else{

        Flowable<Bean> flowable = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class).get2(map);
        presenter.getData1(flowable,tag);
        }
    }
}

//view
//IView

public interface IView {
    void onSuccess(Object o,int tag);
    void onFailed(Exception e);
}

//Home_fragment

public class Home_fragment extends Fragment implements IView {

    private Presenter presenter;
    private Presenter presenter2;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //加载布局
        View view = View.inflate(getActivity(), R.layout.home_layout,null);

        //http://120.27.23.105/product/getProductCatagory?cid=1
        presenter = new Presenter(this);
        Map<String,String> map = new HashMap<>();
        map.put("cid","1");
        presenter.get("http://120.27.23.105/",map,1);



        presenter2 = new Presenter(this);
        Map<String,String> map2 = new HashMap<>();
        map.put("cid","5");
        presenter2.get("http://120.27.23.105/",map,2);



        //返回布局
        return view;
    }



    @Override
    public void onSuccess(Object o, int tag) {

        if(tag==1)
        {
            Bean b = (Bean)o;
            List<Bean.DataBean> data = b.getData();
            Log.i("TAG",data.size()+"");

        }else if(tag==2){

            Bean b = (Bean)o;
            List<Bean.DataBean> data = b.getData();
            Log.i("TAG2",data.size()+"");

        }





    }

    @Override
    public void onFailed(Exception e) {

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值