Android:Retrofit+RxJava+MVP+网络请求判断
程序员文章站
2022-07-08 19:02:58
Android:Retrofit+RxJava+MVP+网络请求判断
//依赖
compile 'io.reactivex.rxjava2:rxjava:2.1...
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 { //https://120.27.23.105/product/getProductCatagory?cid=1 @GET("product/getProductCatagory") Flowable get(@QueryMap Map map); @GET("product/getProductCatagory") Flowable get2(@QueryMap Map 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 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 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 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 map, int tag) { if(tag==1) { Flowable flowable = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class).get(map); presenter.getData(flowable,tag); }else{ Flowable 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); //https://120.27.23.105/product/getProductCatagory?cid=1 presenter = new Presenter(this); Map map = new HashMap<>(); map.put("cid","1"); presenter.get("https://120.27.23.105/",map,1); presenter2 = new Presenter(this); Map map2 = new HashMap<>(); map.put("cid","5"); presenter2.get("https://120.27.23.105/",map,2); //返回布局 return view; } @Override public void onSuccess(Object o, int tag) { if(tag==1) { Bean b = (Bean)o; List data = b.getData(); Log.i("TAG",data.size()+""); }else if(tag==2){ Bean b = (Bean)o; List data = b.getData(); Log.i("TAG2",data.size()+""); } } @Override public void onFailed(Exception e) { } }
上一篇: 不放弃的仞性大标准要素
下一篇: 两位*女人的*对话雷倒人