Retrofit+RxJava+MVP+網路請求多次判斷
阿新 • • 發佈:2019-01-22
//依賴
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) {
}
}