Retiofit封装+RxJava2+MVP结合操作使用

一.在使用Retiofit和RxJava时候导入依赖

    //retrofit
    compile 'com.squareup.retrofit2:retrofit:+'
    compile 'com.squareup.retrofit2:converter-gson:+'
    //Rxjava2
    compile 'io.reactivex.rxjava2:rxjava:+'
    compile 'io.reactivex.rxjava2:rxandroid:+'
    //让retrofit支持Rxjava2
    compile 'com.squareup.retrofit2:adapter-rxjava2:+'
二. Retiofit的封装.配合拦截器

public class RetiofitUtils {
    public static final String BASE_URL ="http://120.27.23.105/";
    private final Retrofit mRetrofit;

    public static class SINGLE_HOLDER{
        public static final RetiofitUtils INSTANCE = new RetiofitUtils(BASE_URL);
    }

    public static RetiofitUtils getInstance(){
        return SINGLE_HOLDER.INSTANCE;
    }

    private RetiofitUtils(String baseUrl){
        mRetrofit = buildRetrofit();
    }

    private OkHttpClient buildOkHttpClient(){
        return new OkHttpClient.Builder()
                .connectTimeout(10000, TimeUnit.MILLISECONDS)
                .addInterceptor(new Intercept())
                .build();
    }

    private Retrofit buildRetrofit(){
        return new Retrofit.Builder()
                .client(buildOkHttpClient())
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    public  T create(Class tClass){
        return mRetrofit.create(tClass);
    }

}


三.拦截器

public class Intercept implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        HttpUrl url=original.url().newBuilder()
                .addQueryParameter("source","android")
                .build();
        //添加请求头
        Request request = original.newBuilder()
                .url(url)
                .build();
        return chain.proceed(request);
    }
}


四.写入RetiofitVpi,可以复用

public interface IRetiofitVip {
    @GET("user/updateNickName")
    Observable updateNick(@Query("uid") String uid, @Query("nickname") String nickname);}

五.写入Model层,

public class Model implements IModel{

    //修改
    public Observable nicknameUpdate(String uid,String nickname){
        Log.e("RxJava","LoginModel");
        IRetiofitVip iRetiofitVip = RetiofitUtils.getInstance().create(IRetiofitVip.class);
        return iRetiofitVip.updateNick(uid,nickname);
    }}

六.写入Presenter层

首先写入Presenter泛型抽象类

public abstract class IPresenter {
    protected T view;

    public IPresenter(T view){
        this.view = view;
        init();
    }

    protected abstract void init();
}


然后写入Presenter

public class NicknamePresenter extends IPresenter {
    private Model model;
    public NicknamePresenter(INicknameView view) {
        super(view);
    }

    @Override
    protected void init() {
        model = new Model();
    }

    public void nicknameUpdate(String uid,String nickname){
        final Observable observable = model.nicknameUpdate(uid, nickname);
        observable
                //需要在io子线程联网
                .subscribeOn(Schedulers.io())
                //需要在主线程更新UI
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(NicknameBean nicknameBean) throws Exception {
                        view.nicknameSuccess(nicknameBean);
                    }
                }, new Consumer() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.nicknameFaild(throwable);
                    }
                });
    }
}


七.写入View层

首先写个泛型view层接口,供View使用


public interface IView {
}
然后写View接口继承view使用

public interface INicknameView extends IView{
    void nicknameSuccess (NicknameBean nicknameBean);
    void nicknameFaild(Throwable e);
}

八.View层实现接口获得数据,进行UI操作


MVP由个别接口举例展示










你可能感兴趣的:(android)