简书:https://www.jianshu.com/p/8200efaf4e41
简单 记录对Rxjava2+Retrofit2+Okhttp的使用。
可通过 okhttp 来设置一些参数,如统一参数 拦截器,超时时间等。
一个简单的单例:
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new ReceivedCookiesInterceptor())
.addInterceptor(new AddCookiesInterceptor())
.readTimeout(HttpConfig.TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(HttpConfig.TIMEOUT,TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(HttpConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
api = retrofit.create(API.class);
}
private static class singleInstance {
private static final RetrofitNetWork instance = new RetrofitNetWork();
}
public static RetrofitNetWork getInstane() {
return singleInstance.instance;
}
public API api() {
return api;
}
接下来对请求放回的结果写一个基类,一般的请求如下:
{
"data": ...,
"code": 0,
"msg": " "
}
基类:
public class BaseEntity<T> {
public int code;
public String msg;
public T data;
...get set 方法
线程的统一调度:
public static <T> ObservableTransformer<T, T> setThread() {
return new ObservableTransformer<T, T>() {
@Override
public ObservableSource<T> apply(Observable<T> observable) {
return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
};
}
请求封装:
public abstract class BaseObserver<T> implements Observer<BaseEntity<T>> {
protected Context mContext;
public BaseObserver(Context context) {
this.mContext = context;
}
public BaseObserver() {
}
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(BaseEntity<T> value) {
ToastUtil.showToast(mContext, value.getErrorMsg());
//返回码是否正确
if (value.isSuccess()) {
onSuccess(value);
} else {
onFailure(value);
}
}
@Override
public void onError(Throwable e) {
if (e instanceof HttpException) {
//网络问题
onException(ExceptionReason.BAD_NETWORK);
} else if (e instanceof ConnectException
|| e instanceof UnknownHostException) {
//连接错误
onException(ExceptionReason.CONNECT_ERROR);
} else if (e instanceof InterruptedIOException) {
//连接超时
onException(ExceptionReason.CONNECT_TIMEOUT);
} else if (e instanceof JsonParseException
|| e instanceof JSONException
|| e instanceof ParseException) {
//解析错误
onException(ExceptionReason.PARES_ERROR);
} else {
//未知错误
onException(ExceptionReason.UNKNOWN_ERROR);
}
}
@Override
public void onComplete() {
}
/**
* 成功
*
* @param t
* @throws Exception
*/
protected abstract void onSuccess(BaseEntity<T> t);
/**
* 失败
*
* @param t
*/
protected abstract void onFailure(BaseEntity<T> t);
/**
* 请求异常
*
* @param reason
*/
public void onException(ExceptionReason reason) {
switch (reason) {
case PARES_ERROR:
ToastUtil.showToast(mContext, "数据解析错误");
break;
case BAD_NETWORK:
ToastUtil.showToast(mContext, "网络连接出现问题");
break;
case CONNECT_ERROR:
ToastUtil.showToast(mContext, "网络连接错误");
break;
case CONNECT_TIMEOUT:
ToastUtil.showToast(mContext, "网络连接超时");
break;
case UNKNOWN_ERROR:
ToastUtil.showToast(mContext, "未知错误");
break;
default:
break;
}
}
/**
* 请求网络失败原因
*/
public enum ExceptionReason {
/**
* 解析数据失败
*/
PARES_ERROR,
/**
* 网络问题
*/
BAD_NETWORK,
/**
* 连接错误
*/
CONNECT_ERROR,
/**
* 连接超时
*/
CONNECT_TIMEOUT,
/**
* 未知错误
*/
UNKNOWN_ERROR,
}
}
只抽象 成功和失败的方法,这样请求的时候就不用又是一大堆方法。
简单实用:
RetrofitNetWork.getInstane()
.api()
.getLogin("xxx","xxx")
.compose(SchedulersConfig.<BaseEntity<LoginEntiy>>setThread())
.subscribe(new BaseObserver<LoginEntiy>() {
@Override
protected void onSuccess(BaseEntity<LoginEntiy> t) {
}
@Override
protected void onFailure(BaseEntity<LoginEntiy> t) {
}
});