Simple package of Retrofit2.0 and RxJava2.0

一, first add project dependencies:

    implementation "com.squareup.okhttp3:logging-interceptor:$var.loggingInterceptor"
    implementation "com.squareup.okhttp3:okhttp:$var.okhttp"
    implementation "com.squareup.retrofit2:retrofit:$var.retrofit"
    implementation "com.google.code.gson:gson:$var.gson"
    implementation "com.squareup.retrofit2:converter-gson:$var.converterGson"
    implementation "com.squareup.retrofit2:adapter-rxjava:$var.adapterRxjava"
    implementation "io.reactivex:rxandroid:$var.rxandroid"
    implementation "io.reactivex:rxjava:$var.rxjava"
    implementation "com.squareup.retrofit2:converter-scalars:$var.converterScalars"

二, create interface call class

public interface APIService {

    /**
     * post mode request network parameters
     *
     *@paramtoken request parameters
     * @return
     */
    @FormUrlEncoded
    @POST("AppInfo.php")
    Observable<BaseEntity<AppInfo>> getAppInfo(@Field("token") String token);

    @FormUrlEncoded
    @POST("FunctionList.php")
    Observable<BaseEntity<List<FunctionItem>>> getFunctionList(@Field("token") String token);
}

三, package retrofit

public class HttpMethods {

    private static final String BASE_URL = "http://xxxxxxxxxxxxxx";
    private static final long DEFAULT_TIME = 10000L;
    private Retrofit retrofit;
    private final String TAG = HttpMethods.class.getSimpleName();

    private static class SingletonHolder {
        private static final HttpMethods INSTANCE = new HttpMethods();
    }

    private HttpMethods() {
        // intercept and print log informationHttpLoggingInterceptor httpLoggingInterceptor =new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                LogUtils.setTag(TAG);
                LogUtils.json(message);
            }
        }).setLevel(HttpLoggingInterceptor.Level.BODY);
        //Initialize OKHttpOkHttpClient okHttpClient =new OkHttpClient.Builder()
                .connectTimeout(DEFAULT_TIME, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIME, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIME, TimeUnit.SECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .retryOnConnectionFailure(true)
                .build();
        //Initialize Retrofit|| |retrofit =
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    public static HttpMethods getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public <T> T createService(Class<T> clazz) {
        return retrofit.create(clazz);
    }
}

四, return data unified package

public class BaseEntity<T> implements Serializable{
    private int code;
    private String message;
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

五, unified thread scheduling

public class DefaultTransformer<T> implements Observable.Transformer<T, T> {


    @Override
    public Observable<T> call(Observable<T> tObservable) {
        return tObservable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<T, T>() {
                    @Override
                    public T call(T t) {
                        BaseEntity<T> baseEntity = (BaseEntity<T>) t;
                        if (baseEntity.getCode() != 200) {
                            throw new APIException(baseEntity.getCode(), baseEntity.getMessage());
                        }
                        return t;
                    }
                });
    }
    public static <T> DefaultTransformer<T> create(){
        return new DefaultTransformer<>();
    }
}

六, return error unified processing

public abstract class APISubscriber<T> extends Subscriber<T> {

    private final String TAG = APISubscriber.class.getSimpleName();
    private QMUITipDialog tipDialog;

    protected APISubscriber() {

    }

    protected APISubscriber(Context context) {
        QMUITipDialog.Builder builder = new QMUITipDialog.Builder(context);
        builder.setTipWord("Loading");
        builder.setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING);
        tipDialog = builder.create();
        tipDialog.setCancelable(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (tipDialog != null) {
            tipDialog.show();
        }
    }

    @Override
    public void onCompleted() {
        if (tipDialog != null && tipDialog.isShowing()) {
            tipDialog.dismiss();
        }
    }


    @Override
    public void onError(Throwable e) {
        if (tipDialog != null && tipDialog.isShowing())
            tipDialog.dismiss();
        if (e instanceof APIException) {
            LogUtils.e("handling the error returned by the server");
        }else if (e instanceof ConnectException) {
            LogUtils.e("Network exception, please check the network ");
        }else if (e instanceof TimeoutException || e instanceof SocketException) {
            LogUtils.e("Network exception, please check the network ");
        }else if (e instanceof JsonSyntaxException) {
            LogUtils.e("Data parsing exception");
        }else {
            LogUtils.e("Server-side exception");
        }
        e.printStackTrace();
    }
}

七,用

    /**
     * Get APP version information
     *
     * @param aty
     */
    public void getAppInfo(final FunctionListAty aty) {

        final Subscription subscription = HttpMethods.getInstance()
                .createService(APIService.class)
                .getAppInfo("123")
                .compose(DefaultTransformer.<BaseEntity<AppInfo>>create())
                .subscribe(new APISubscriber<BaseEntity<AppInfo>>(aty) {
                    @Override
                    public void onNext(BaseEntity<AppInfo> appInfoBaseEntity) {
                        AppInfo appInfo = appInfoBaseEntity.getData();
                        if (appInfo != null) {
                            Constants.DEFAULT_URL = appInfo.getApp_download_url();
                            new UpdateManager(aty, appInfo).checkUpdate();
                        }
                    }
                });
        aty.addSubscription(subscription);
    }

八, Request multiple interfaces in succession

 /**
     * Multiple request merges
     */
    public void getRequest(final FunctionListAty aty) {
        final APIService apiService = HttpMethods.getInstance().createService(APIService.class);
        Observable<BaseEntity<AppInfo>> appInfo = apiService.getAppInfo("123");
        Observable<BaseEntity<List<FunctionItem>>> functionList = apiService.getFunctionList("123");
        final Subscription subscription = Observable.mergeDelayError(appInfo, functionList)
                .compose(DefaultTransformer.create())
                .subscribe(new APISubscriber<Object>(aty) {

                    @Override
                    public void onNext(Object o) {
                        if (o instanceof BaseEntity) {
                            BaseEntity baseEntity = (BaseEntity) o;
                            Object data = baseEntity.getData();
                            if (data != null) {
                                if (data instanceof AppInfo) {
                                    LogUtils.i("Request success---version information");
                                    AppInfo appInfo = (AppInfo) data;
                                    Constants.DEFAULT_URL = appInfo.getApp_download_url();new UpdateManager(aty, appInfo).checkUpdate();
                                } else if (data instanceof List) {
                                    LogUtils.i("Request success---list");
                                    List<FunctionItem> functionItemList = (List<FunctionItem>) data;
                                    functionItems.addAll(functionItemList);
                                }
                            }
                        }
                    }
                });
        aty.addSubscription(subscription);
    }