這篇文章給大家分享的是有關(guān)RxJava2+Retrofit2網(wǎng)絡(luò)框架如何封裝的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括溫泉網(wǎng)站建設(shè)、溫泉網(wǎng)站制作、溫泉網(wǎng)頁(yè)制作以及溫泉網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,溫泉網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到溫泉省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
需求
封裝之前要先明白需要滿足哪些需求。
RxJava2銜接Retrofit2
Retrofit2網(wǎng)絡(luò)框架異常的統(tǒng)一處理
兼容fastjson(可選)
RxJava2內(nèi)存泄漏的處理
異步請(qǐng)求加入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
先封裝一個(gè)網(wǎng)絡(luò)框架的管理類,方便調(diào)用
public class RxHttp { private final String BASE_URL = "https://github.com/"; private MapmRetrofitMap = 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要以/作為結(jié)尾 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風(fēng)格接口
public interface SyncServerService { @GET("service/mobile/IsLatestVersion.ashx") Observable> getLatestVersion(@Query("SoftwareID") String SoftwareID, @Query("ClientVersion") String ClientVersion); }
服務(wù)端返回的基本類型,在導(dǎo)入類的時(shí)候特別需要注意區(qū)分該Response類型
public class Response{ public int ret;//約定 -1為server返回?cái)?shù)據(jù)異常 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的支持
由于項(xiàng)目中采用了fastjson,square尚未實(shí)現(xiàn)對(duì)fastjson的支持,但是保留了代碼的擴(kuò)展,這邊可以自己封裝一下fastjson的轉(zhuǎn)換器。
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 ConverterresponseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { return new FastJsonResponseBodyConvert<>(type); } }
final class FastJsonRequestBodyConverterimplements Converter { 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 FastJsonResponseBodyConvertimplements Converter { private Type mType; public FastJsonResponseBodyConvert(Type type) { this.mType = type; } @Override public T convert(ResponseBody value) throws IOException { return JSON.parseObject(value.string(), mType); } }
數(shù)據(jù)返回統(tǒng)一處理
public abstract class BaseObserverimplements Observer > { @Override public final void onNext(@NonNull Response result) { if (result.getRet() == -1) { onFailure(new Exception(result.getMsg()), result.getMsg());//該異??梢詤R報(bào)服務(wù)端 } 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 = "未知錯(cuò)誤"; if (e instanceof UnknownHostException) { errorMsg = "網(wǎng)絡(luò)不可用"; } else if (e instanceof SocketTimeoutException) { errorMsg = "請(qǐng)求網(wǎng)絡(luò)超時(shí)"; } 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 = "數(shù)據(jù)解析錯(cuò)誤"; } return errorMsg; } private static String convertStatusCode(HttpException httpException) { String msg; if (httpException.code() >= 500 && httpException.code() < 600) { msg = "= 400 && httpException.code() < 500) { msg = "服務(wù)器無(wú)法處理請(qǐng)求"; } else if (httpException.code() >= 300 && httpException.code() < 400) { msg = "請(qǐng)求被重定向到其他頁(yè)面"; } else { msg = httpException.message(); } return msg; } }
異步請(qǐng)求加入Loading Dialog
這個(gè)時(shí)候我們可以根據(jù)自己項(xiàng)目中統(tǒng)一封裝的dialog自行擴(kuò)展BaseObserver
public abstract class ProgressObserverextends BaseObserver { 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(); } } }
加入調(diào)度類,方便調(diào)用線程切換和解決內(nèi)存泄漏的問(wèn)題
public class RxSchedulers { public staticObservableTransformer observableIO2Main(final Context context) { return upstream -> { Observable observable = upstream.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); return composeContext(context, observable); }; } public static ObservableTransformer observableIO2Main(final RxFragment fragment) { return upstream -> upstream.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()).compose(fragment. bindToLifecycle()); } private static ObservableSource composeContext(Context context, Observable 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; } } }
講了那么多,那么如何使用這個(gè)封裝呢?下面來(lái)看下如何使用。
RxHttp.getInstance().getSyncServer().getLatestVersion("1", "1.0.0") .compose(RxSchedulers.observableIO2Main(this)) .subscribe(new ProgressObserver(this) { @Override public void onSuccess(String result) { Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Throwable e, String errorMsg) { } });
感謝各位的閱讀!關(guān)于“RxJava2+Retrofit2網(wǎng)絡(luò)框架如何封裝”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!