標籤:泛型類 lin images value null master read 注意 構建
App 組件化/模組化之路——如何封裝網路請求架構
在 App 開發中網路請求是每個開發人員必備的開發庫,也出現了許多優秀開源的網路請求庫。例如
- okhttp
- retrofit
- android-async-http
這些網路請求庫很大程度上提高程式猿的編碼效率。但是隨著業務的發展,App 變得越來越大,我們將這些網路請求庫加入到項目中直接使用,對我們業務類的入侵是非常強的。如果要進行業務分離時,這些網路請求代碼將是一個阻止我們進一步工作的絆腳石。對開發人員來說是非常痛苦的。
因此我們構建的網路請求架構要可以解決以下問題:
- 分離業務與網路請求代碼
- 網路程式庫可以很容易的被替換
- 網路程式庫可以很方便的複用
所以在 App 組件化/模組化開發架構思路 一文中,我們把網路請求作為核心層的一個組件。
封裝第三方網路請求介面
一般來說,目前絕大部分 App 的資料請求都是使用 HTTP 協議,而資料交換的協議使用 json 格式。因此可以封裝一個通用的請求介面。(當然還有其他一些協議,例如的 mars ,但是封裝的思路是一致的,本文為了簡單說明,暫時使用通用網路請求架構,不排除以後會對 mars 的封裝)
首先預覽一下架構結構
IRequest
這個類封裝了網路請求的通用介面,定義請求介面 doRequest() 、擷取請求串連 getUrl() 、擷取要求方法 getHttpMethod() 等。
public interface IRequest { enum HttpMethod { GET, POST, PUT, DELETE } //... 為了減少代碼的篇幅,省略一些對本文說明不重要的片段,本文代碼可以在 //https://github.com/wecodexyz/Componentization 擷取到 void addParams(Map<String, String> params); String getUrl(); Pair<Integer, String> doRequest(); boolean isSupportCache(); void addHeader(String key, String value); HttpMethod getHttpMethod(); //... 為了減少代碼的篇幅,省略一些對本文說明不重要的片段,本文代碼可以在 //https://github.com/wecodexyz/Componentization 擷取到}
Request
這個類是個抽象類別,對 IRequest 的實現。目前是一個簡單封裝的實現。
RequestWrapper
這個類是一個泛型類,繼承於 Request 並對第三方請求庫的封裝。例如本文就是對 okhttp 的封裝,而泛型 T 對象就是請求得到的具體資料類型。如果要對其他請求庫進行封裝,就可以參考這個類的實現。
注意這個類封裝是純粹的網路請求,不應該包含業務類相關的代碼。否則無解決上文提出的三個問題。
public abstract class RequestWrapper extends Request { //... 為了減少代碼的篇幅,省略一些對本文說明不重要的片段,本文代碼可以在 //https://github.com/wecodexyz/Componentization 擷取到 @Override public Pair<Integer, String> doRequest() { Pair<Integer, String> result = new Pair<>(ERROR_NETWORK, ""); okhttp3.Request request = null; if (getHttpMethod() == HttpMethod.POST) { request = requestBuilder().url(getUrl()).post(requestBody()).build(); } else { request = requestBuilder().url(getUrlWithParams()).build(); } try { Response response = mClient.newCall(request).execute(); if (response.isSuccessful()) { result = new Pair<>(response.code(), response.body().string()); } else { result = new Pair<>(response.code(), response.message()); } } catch (IOException e) { Log.e(TAG, e.getMessage()); } return result; } //... 為了減少代碼的篇幅,省略一些對本文說明不重要的片段,本文代碼可以在 //https://github.com/wecodexyz/Componentization 擷取到}
關鍵的代碼是在 doRequest() 方法中,該方法實現了網路請求的代碼,返回一個 Pair<Integer,String>對象,該對象的 first 屬性是一個請求 code ,用於標識網路請求碼(即是網路請求返回的200,404,301等)。而 second 就是網路請求的資料。
BaseTextRequest
這個類就是網路請求架構提供給業務類使用的一個介面。本文一開始就提出來 json 作為互動資料請求的協議。那麼此類的封裝就有利於業務資料的訪問。
public abstract class BaseTextRequest<T> extends RequestWrapper { public BaseTextRequest(Context context) { super(context); } public Flowable<T> request() { return Flowable.fromCallable(new Callable<Pair<Integer, String>>() { @Override public Pair<Integer, String> call() throws Exception { Pair<Integer, String> result = doRequest(); return result; } }).flatMap(new Function<Pair<Integer, String>, Publisher<T>>() { @Override public Publisher<T> apply(@NonNull Pair<Integer, String> pair) throws Exception { if (isSuccessful(pair.first)) { return Flowable.just(onRequestFinish(pair.second)); } return Flowable.just(onRequestError(pair.first, pair.second)); } }); } @Override public boolean isSupportCache() { return true; } protected abstract T onRequestFinish(String result); protected abstract T onRequestError(int code, String message);}
由於請求網路是耗時的操作,rxjava2 來實現網路請求非同步作業。 request 是對 RequestWrapper.doRequest() 方法的封裝,並得到一個 Flowable 對象。同時定義了 onRequestFinish() 和 onRequestError() 兩個方法。
這兩個方法就是具體業務類要處理的邏輯。
SimpleTextRequest
假設有一個請求業務資料介面,返回資料是一個字串。那麼我們使用我們的架構就是這樣來使用。本文例子是請求我們項目中的 README.md 的內容。用起來非常簡單,只要繼承於 BaseTextRequest,並實現 getUrl() 、 onRequestFinish() onRequestError()、 getHttpMethod() 這幾個方法。
注意嚴格來說這是一個業務類,所以是不應該放在 core 目錄下的。
public class SimpleTextRequest extends BaseTextRequest<String> { public SimpleTextRequest(Context context, Map<String, String> params) { super(context); addParams(params); } @Override public String getUrl() { return "https://raw.githubusercontent.com/wecodexyz/Componentization/master/README.md"; } @Override public HttpMethod getHttpMethod() { return HttpMethod.GET; } @Override protected String onRequestFinish(String result) { //這裡可以實現對 json 資料的解析,例如使用 JSONObject //對象解析具體的業務 return result; } @Override protected String onRequestError(int code, String message) { return message; }}
測試請求架構
request = new SimpleTextRequest(this, null); request.request() .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<String>() { @Override public void accept(@NonNull String s) throws Exception { textView.setText(s); //這裡返回介面請求的資料 } }, new Consumer<Throwable>() { @Override public void accept(@NonNull Throwable throwable) throws Exception { textView.setText(throwable.getMessage()); } });
本文啟動並執行結果
項目地址:https://github.com/wecodexyz/Componentization
關注我們,可以擷取更多
App 組件化/模組化之路——如何封裝網路請求架構