RxJava2+Retrofit 简单封装

依赖

//Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

//RxJava2
    implementation 'io.reactivex.rxjava2:rxjava:2.0.2'
    implementation'io.reactivex.rxjava2:rxandroid:2.0.2'

Retrofit 简单封装

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitHttp {
    private Retrofit retrofit;
    //你的Retrofit接口
    private ServerInterface server;    
    private RetrofitHttp(){
        retrofit = new Retrofit.Builder()
                .baseUrl("baseUrl")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient())
                .build();
        server = retrofit.create(ServerInterface.class);
    }
    private static class RetrofitHttpInstance{
        private static final RetrofitHttp instance = new RetrofitHttp();
    }

    private static RetrofitHttp getInstance(){
        return RetrofitHttpInstance.instance;
    }

    //你的Retrofit接口
    public static ServerInterface getServer(){
        return getInstance().server;
    }
}

RxJava2 简单封装

import android.util.Log;

import com.crbb88.myapplication.rxJava.OnBaseDataListener;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RxBase {
    private static class ObservableInstance{
        private static final RxBase instance = new RxBase();
    }
    private RxBase(){ }
    public static RxBase getInstance(){
        return ObservableInstance.instance;
    }

    public  void subscribeRetrofit(Observable observable, final OnBaseDataListener listener){
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(T value) {
                        listener.success(value);
                        disposable.dispose();
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.error(e.getMessage(), e);
                        Log.e("RxBase——————Error : ", e.getLocalizedMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.d("RxBase——————Complete : ", "观察请求完毕");
                    }
                });
    }

    public  void subscribeObserver(final OnBaseDataListener listener){
        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                Log.i("subscribe", "subscribe");
                /*emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);*/
            }
        }).subscribe(new Observer() {
            private Disposable disposable;

            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
            }

            @Override
            public void onNext(T value) {
                listener.success(value);
                disposable.dispose();
            }

            @Override
            public void onError(Throwable e) {
                listener.error(e.getMessage(), e);
                Log.e("RxBase——————Error : ", e.getLocalizedMessage());
            }

            @Override
            public void onComplete() {
                Log.d("RxBase——————Complete : ", "观察请求完毕");
            }
        });
    }
}

数据传递接口

public interface OnBaseDataListener   {
    void success(T value);
    void error(String msg, Throwable e);
}

使用方法 - 贴个Model层代码

//获取公告数据 -- 实现数据接口 OnBaseDataListener
public static void getNotice(OnBaseDataListener listener){
        RxBase.getInstance().subscribeRetrofit(RetrofitHttp.getServer().getNotice(), listener);
}

 

你可能感兴趣的:(Android)