MVp封装RxJava+Retrofit

封装RxJava+Retrofit导入依赖

compile 'io.reactivex.rxjava2:rxjava:2.1.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
//工具类
public interface ApiService {
    @GET("api/data/Android/10/1")
    Flowable<AndroidBean> getAndroid(@QueryMap Map<String, String> map);
}
public class RetrofitUtils {
    private static volatile RetrofitUtils instance;
    private final Retrofit retrofit;

    private RetrofitUtils() {
        retrofit = new Retrofit.Builder()
                .baseUrl("http://gank.io/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static RetrofitUtils getInstance() {
        if (instance == null) {
            synchronized (RetrofitUtils.class) {
                if (instance == null) {
                    instance = new RetrofitUtils();
                }
            }
        }
        return instance;
    }
    public Retrofit getRetrofit() {
        return retrofit;
    }
}
//model层
public interface IModel {
    void get(Map<String,String> map);
}

public class AndroidModel implements IModel{
    private AndroidPresenter presenter;

    public AndroidModel(AndroidPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void get(Map<String, String> map) {
        Flowable<AndroidBean> android = RetrofitUtils.getInstance().getRetrofit().create(ApiService.class).getAndroid(map);

        presenter.getData(android);
    }
} 
//view层
public interface IView {
    void onSuccess(Object o);
    void onFailed(Exception e);
}

//presenter层

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

public class AndroidPresenter implements BasePresenter {

    private IView iv;
    private DisposableSubscriber subscriber;

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

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

    public void getData(Flowable flowable) {
        subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        iv.onSuccess(o);
                        Log.e("CDM", "onNext: o" + "数据请求成功");
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed((Exception) t);
                        Log.e("CDM", "onNext: o" + "数据请求失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

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

//最后在mainActivity里实现ivew接口,调用p层

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值