国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳解RxJava2 Retrofit2 網絡框架簡潔輕便封裝

2019-10-22 18:20:53
字體:
來源:轉載
供稿:網友

前言

RxJava2Retrofit2火了有一段時間了,前段時間給公司的項目引入了這方面相關的技術,在此記錄一下相關封裝的思路。

需求

封裝之前要先明白需要滿足哪些需求。

  1. RxJava2銜接Retrofit2
  2. Retrofit2網絡框架異常的統一處理
  3. 兼容fastjson(可選)
  4. RxJava2內存泄漏的處理
  5. 異步請求加入Loading Dialog

依賴

implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' implementation 'io.reactivex.rxjava2:rxjava:2.1.3' implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' implementation 'com.squareup.okhttp3:okhttp:3.9.0' implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.0' implementation 'com.alibaba:fastjson:1.1.59.android'//可選其它框架比如Gson

RxJava2銜接Retrofit2

先封裝一個網絡框架的管理類,方便調用

public class RxHttp { private final String BASE_URL = "https://github.com/"; private Map<String, Retrofit> mRetrofitMap = new HashMap<>(); private RxHttp() { } /**  * 單例模式  * @return  */ public static RxHttp getInstance() {  return RxHttpHolder.sInstance; } private static class RxHttpHolder{  private final static RxHttp sInstance = new RxHttp(); } public Retrofit getRetrofit(String serverUrl) {  Retrofit retrofit;  if (mRetrofitMap.containsKey(serverUrl)) {   retrofit = mRetrofitMap.get(serverUrl);  } else {   retrofit = createRetrofit(serverUrl);   mRetrofitMap.put(serverUrl, retrofit);  }  return retrofit; } public SyncServerService getSyncServer(){  return getRetrofit(BASE_URL).create(SyncServerService.class); } /**  *  * @param baseUrl baseUrl要以/作為結尾 eg:https://github.com/  * @return  */ private Retrofit createRetrofit(String baseUrl) {  OkHttpClient client = new OkHttpClient().newBuilder()    .readTimeout(30, TimeUnit.SECONDS)    .connectTimeout(30, TimeUnit.SECONDS)    .retryOnConnectionFailure(true)    .build();  return new Retrofit.Builder()    .baseUrl(baseUrl)    .addConverterFactory(FastJsonConverterFactory.create())    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())    .client(client)    .build(); }}

Restful風格接口

public interface SyncServerService { @GET("service/mobile/IsLatestVersion.ashx") Observable<Response<String>> getLatestVersion(@Query("SoftwareID") String SoftwareID,             @Query("ClientVersion") String ClientVersion);}

服務端返回的基本類型,在導入類的時候特別需要注意區分該Response類型

public class Response<T> { public int ret;//約定 -1為server返回數據異常 200為正常范圍 public String msg; public T data; public int getRet() {  return ret; } public void setRet(int ret) {  this.ret = ret; } public String getMsg() {  return msg; } public void setMsg(String msg) {  this.msg = msg; } public T getData() {  return data; } public void setData(T data) {  this.data = data; }}

fastjson的支持

由于項目中采用了fastjson,square尚未實現對fastjson的支持,但是保留了代碼的擴展,這邊可以自己封裝一下fastjson的轉換器。

public class FastJsonConverterFactory extends Converter.Factory { private final SerializeConfig mSerializeConfig; private FastJsonConverterFactory(SerializeConfig serializeConfig) {  this.mSerializeConfig = serializeConfig; } public static FastJsonConverterFactory create() {  return create(SerializeConfig.getGlobalInstance()); } public static FastJsonConverterFactory create(SerializeConfig serializeConfig) {  return new FastJsonConverterFactory(serializeConfig); } @Override public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {  return new FastJsonRequestBodyConverter<>(mSerializeConfig); } @Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {  return new FastJsonResponseBodyConvert<>(type); }}
final class FastJsonRequestBodyConverter<T> implements Converter<T, RequestBody> { private final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8"); private SerializeConfig mSerializeConfig; public FastJsonRequestBodyConverter(SerializeConfig serializeConfig) {  this.mSerializeConfig = serializeConfig; } @Override public RequestBody convert(T value) throws IOException {  return RequestBody.create(MEDIA_TYPE, JSON.toJSONBytes(value, mSerializeConfig)); }}
final class FastJsonResponseBodyConvert<T> implements Converter<ResponseBody, T> { private Type mType; public FastJsonResponseBodyConvert(Type type) {  this.mType = type; } @Override public T convert(ResponseBody value) throws IOException {  return JSON.parseObject(value.string(), mType); }}

數據返回統一處理

public abstract class BaseObserver<T> implements Observer<Response<T>> { @Override public final void onNext(@NonNull Response<T> result) {  if (result.getRet() == -1) {   onFailure(new Exception(result.getMsg()), result.getMsg());//該異常可以匯報服務端  } else {   onSuccess(result.getData());  } } @Override public void onError(@NonNull Throwable e) {  onFailure(e, RxExceptionUtil.exceptionHandler(e)); } @Override public void onComplete() { } @Override public void onSubscribe(@NonNull Disposable d) { } public abstract void onSuccess(T result); public abstract void onFailure(Throwable e, String errorMsg);}

下面加入了異常處理類

public class RxExceptionUtil { public static String exceptionHandler(Throwable e){  String errorMsg = "未知錯誤";  if (e instanceof UnknownHostException) {   errorMsg = "網絡不可用";  } else if (e instanceof SocketTimeoutException) {   errorMsg = "請求網絡超時";  } else if (e instanceof HttpException) {   HttpException httpException = (HttpException) e;   errorMsg = convertStatusCode(httpException);  } else if (e instanceof ParseException || e instanceof JSONException    || e instanceof com.alibaba.fastjson.JSONException) {   errorMsg = "數據解析錯誤";  }   return errorMsg; } private static String convertStatusCode(HttpException httpException) {  String msg;  if (httpException.code() >= 500 && httpException.code() < 600) {   msg = "服務器處理請求出錯";  } else if (httpException.code() >= 400 && httpException.code() < 500) {   msg = "服務器無法處理請求";  } else if (httpException.code() >= 300 && httpException.code() < 400) {   msg = "請求被重定向到其他頁面";  } else {   msg = httpException.message();  }  return msg; }}

異步請求加入Loading Dialog

這個時候我們可以根據自己項目中統一封裝的dialog自行擴展BaseObserver

public abstract class ProgressObserver<T> extends BaseObserver<T>{ private MaterialDialog mMaterialDialog; private Context mContext; private String mLoadingText; public ProgressObserver(Context context){  this(context, null); } public ProgressObserver(Context context, String loadingText){  mContext = context;  mLoadingText = loadingText; } @Override public void onSubscribe(@NonNull Disposable d) {  if (!d.isDisposed()) {   mMaterialDialog = new MaterialDialog.Builder(mContext).content(mLoadingText == null ? "正在加載中..."     : mLoadingText).isProgress(true).build();   mMaterialDialog.show();  } } @Override public void onComplete() {  if (mMaterialDialog != null) {   mMaterialDialog.dismiss();  } } @Override public void onError(@NonNull Throwable e) {  super.onError(e);  if (mMaterialDialog != null) {   mMaterialDialog.dismiss();  } }}

加入調度類,方便調用線程切換和解決內存泄漏的問題

public class RxSchedulers { public static <T> ObservableTransformer<T, T> observableIO2Main(final Context context) {  return upstream -> {   Observable<T> observable = upstream.subscribeOn(Schedulers.io())     .observeOn(AndroidSchedulers.mainThread());   return composeContext(context, observable);  }; } public static <T> ObservableTransformer<T, T> observableIO2Main(final RxFragment fragment) {  return upstream -> upstream.subscribeOn(Schedulers.io())    .observeOn(AndroidSchedulers.mainThread()).compose(fragment.<T>bindToLifecycle()); } private static <T> ObservableSource<T> composeContext(Context context, Observable<T> observable) {  if(context instanceof RxActivity) {   return observable.compose(((RxActivity) context).bindUntilEvent(ActivityEvent.DESTROY));  } else if(context instanceof RxFragmentActivity){   return observable.compose(((RxFragmentActivity) context).bindUntilEvent(ActivityEvent.DESTROY));  }else if(context instanceof RxAppCompatActivity){   return observable.compose(((RxAppCompatActivity) context).bindUntilEvent(ActivityEvent.DESTROY));  }else {   return observable;  } }}

講了那么多,那么如何使用這個封裝呢?下面來看下如何使用。

RxHttp.getInstance().getSyncServer().getLatestVersion("1", "1.0.0")    .compose(RxSchedulers.observableIO2Main(this))    .subscribe(new ProgressObserver<String>(this) {     @Override     public void onSuccess(String result) {      Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();     }     @Override     public void onFailure(Throwable e, String errorMsg) {     }    });

是不是封裝后的代碼顯得更為簡潔一點呢?以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彰化县| 珲春市| 宜良县| 和平区| 武安市| 布尔津县| 彩票| 湟源县| 寿光市| 黄石市| 英吉沙县| 喀喇| 泰来县| 山东省| 寻甸| 和林格尔县| 江华| 寿宁县| 扶余县| 胶南市| 理塘县| 绥棱县| 韩城市| 弋阳县| 腾冲县| 内黄县| 玉树县| 北宁市| 太康县| 右玉县| 甘德县| 天台县| 张家港市| 肇庆市| 清苑县| 昭苏县| 府谷县| 兴和县| 巢湖市| 炎陵县| 开平市|