Retrofit+Rxjava網路層的優雅封裝

SunnySky發表於2017-09-29

想必Retrifit+Rxjava的使用,如今已經非常的普及了吧。在此介紹一種比較優雅的有關Retrifit+Rxjava封裝的方法。參考github專案XDroidMvp

原本的步驟應該是這樣,首先要建立OKHttpClient ,在其中新增一些攔截和超時處理,然後建立Retrofit物件並注入OKHttpClient物件,再獲取介面例項Observable物件,然後繫結生命週期(防止記憶體洩漏)並訂閱觀察者Subscriber處理返回資訊。

那現在應該如何封裝,才能比較優雅,並且能夠儘量的解耦呢?

  • 1、提出Retrofits實現類,提供設定超時時間、新增攔截等處理的介面

首先應該將Retrofit這一塊提出來,而建立Retrofit需要注入OKHttpClient,其中有很多與業務相關的處理,比如需要設定超時時間,攔截頭部新增Header等等。那麼這一塊就可以寫一個介面回撥,在外部實現後注入。看一下這一塊的程式碼吧

public class NetMgr {
    private final long connectTimeoutMills = 10 * 1000L;
    private final long readTimeoutMills = 10 * 1000L;
    private NetProvider sProvider = null;
    private static NetMgr instance;
    private Map<String, NetProvider> providerMap = new HashMap<>();
    private Map<String, Retrofit> retrofitMap = new HashMap<>();
    private Map<String, OkHttpClient> clientMap = new HashMap<>();


    public static NetMgr getInstance() {
        if (instance == null) {
            synchronized (NetMgr.class) {
                if (instance == null) {
                    instance = new NetMgr();
                }
            }
        }
        return instance;
    }


    public <S> S get(String baseUrl, Class<S> service) {
        return getInstance().getRetrofit(baseUrl).create(service);
    }

    public void registerProvider(NetProvider provider) {
        this.sProvider = provider;
    }

    public void registerProvider(String baseUrl, NetProvider provider) {
        getInstance().providerMap.put(baseUrl, provider);
    }

    public NetProvider getCommonProvider() {
        return sProvider;
    }

    public void clearCache() {
        getInstance().retrofitMap.clear();
        getInstance().clientMap.clear();
    }

    public Retrofit getRetrofit(String baseUrl) {
        return getRetrofit(baseUrl, null);
    }

    public Retrofit getRetrofit(String baseUrl, NetProvider provider) {
        if (empty(baseUrl)) {
            throw new IllegalStateException("baseUrl can not be null");
        }
        if (retrofitMap.get(baseUrl) != null) {
            return retrofitMap.get(baseUrl);
        }

        if (provider == null) {
            provider = providerMap.get(baseUrl);
            if (provider == null) {
                provider = sProvider;
            }
        }
        checkProvider(provider);

        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();

        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(getClient(baseUrl, provider))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson));

        Retrofit retrofit = builder.build();
        retrofitMap.put(baseUrl, retrofit);
        providerMap.put(baseUrl, provider);

        return retrofit;
    }

    private boolean empty(String baseUrl) {
        return baseUrl == null || baseUrl.isEmpty();
    }

    private OkHttpClient getClient(String baseUrl, NetProvider provider) {
        if (empty(baseUrl)) {
            throw new IllegalStateException("baseUrl can not be null");
        }
        if (clientMap.get(baseUrl) != null) {
            return clientMap.get(baseUrl);
        }

        checkProvider(provider);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.connectTimeout(provider.configConnectTimeoutSecs() != 0
                ? provider.configConnectTimeoutSecs()
                : connectTimeoutMills, TimeUnit.SECONDS);
        builder.readTimeout(provider.configReadTimeoutSecs() != 0
                ? provider.configReadTimeoutSecs() : readTimeoutMills, TimeUnit.SECONDS);

        builder.writeTimeout(provider.configWriteTimeoutSecs() != 0
                ? provider.configReadTimeoutSecs() : readTimeoutMills, TimeUnit.SECONDS);
        CookieJar cookieJar = provider.configCookie();
        if (cookieJar != null) {
            builder.cookieJar(cookieJar);
        }
        provider.configHttps(builder);

        RequestHandler handler = provider.configHandler();
        if (handler != null) {
            builder.addInterceptor(new NetInterceptor(handler));
        }

        Interceptor[] interceptors = provider.configInterceptors();
        if (!empty(interceptors)) {
            for (Interceptor interceptor : interceptors) {
                builder.addInterceptor(interceptor);
            }
        }

        if (provider.configLogEnable()) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(loggingInterceptor);
        }

        OkHttpClient client = builder.build();
        clientMap.put(baseUrl, client);
        providerMap.put(baseUrl, provider);

        return client;
    }

    private boolean empty(Interceptor[] interceptors) {
        return interceptors == null || interceptors.length == 0;
    }

    private void checkProvider(NetProvider provider) {
        if (provider == null) {
            throw new IllegalStateException("must register provider first");
        }
    }

    public Map<String, Retrofit> getRetrofitMap() {
        return retrofitMap;
    }

    public Map<String, OkHttpClient> getClientMap() {
        return clientMap;
    }

}複製程式碼
  • 2、實現NetProvider介面並注入

NetMgr就是一個Retrofit的實現類,然後NetProvider是一個介面,需要在外部去實現,然後注入。再看一下NetProvider的實現類BaseNetProvider

public class BaseNetProvider implements NetProvider {

    private static final long CONNECT_TIME_OUT = 30;
    private static final long READ_TIME_OUT = 180;
    private static final long WRITE_TIME_OUT = 30;


    @Override
    public Interceptor[] configInterceptors() {
        return null;
    }

    @Override
    public void configHttps(OkHttpClient.Builder builder) {

    }

    @Override
    public CookieJar configCookie() {
        return null;
    }

    @Override
    public RequestHandler configHandler() {

        return new HeaderHandler();
    }

    @Override
    public long configConnectTimeoutSecs() {
        return CONNECT_TIME_OUT;
    }

    @Override
    public long configReadTimeoutSecs() {
        return READ_TIME_OUT;
    }

    @Override
    public long configWriteTimeoutSecs() {
        return WRITE_TIME_OUT;
    }

    @Override
    public boolean configLogEnable() {
        return BuildConfig.DEBUG;
    }


    private class HeaderHandler implements RequestHandler {

        @Override
        public Request onBeforeRequest(Request request, Interceptor.Chain chain) {
            return chain.request().newBuilder()
                    .addHeader("X-Auth-Token", Constant.accessToken)
                    .addHeader("Authorization", "")
                    .build();
        }

        @Override
        public Response onAfterRequest(Response response, Interceptor.Chain chain)
                throws IOException {
            ApiException e = null;
            if (401 == response.code()) {
                throw new ApiException("登入已過期,請重新登入!");
            } else if (403 == response.code()) {
                throw new ApiException("禁止訪問!");
            } else if (404 == response.code()) {
                throw new ApiException("連結錯誤");
            } else if (503 == response.code()) {
                throw new ApiException("伺服器升級中!");
            } else if (500 == response.code()) {
                throw new ApiException("伺服器內部錯誤!");
            }
            return response;
        }
    }複製程式碼

在BaseNetProvider中實現了連線、讀、寫超時的時間處理,與請求和返回資料的請求頭部處理。然後需要在Application中去注入BaseNetProvider

NetMgr.getInstance().registerProvider(new BaseNetProvider());複製程式碼
  • 3、Observable實現

首先實現一個UseCase的基類,處理公共的使用方法。通過呼叫NetMgr.getInstance().getRetrofit(BuildConfig.BaseUrl).create(getType())來獲取ApiService的例項,然後提供了指定執行緒的基類方法。至於PagingReq是一個分頁模型,方便分頁介面的使用。

public abstract class UseCase<T> {
    //用於分頁請求 
    protected PagingReq pagingReq = new PagingReq();

    protected T ApiClient() {
        return NetMgr.getInstance().getRetrofit(BuildConfig.BaseUrl).create(getType());
    }

    //指定觀察者與被觀察者執行緒
    protected <T> Observable.Transformer<T, T> normalSchedulers() {
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> source) {
                return source.onTerminateDetach().subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    private Class<T> getType() {
        Class<T> entityClass = null;
        Type t = getClass().getGenericSuperclass();
        Type[] p = ((ParameterizedType) t).getActualTypeArguments();
        entityClass = (Class<T>) p[0];
        return entityClass;
    }
}複製程式碼

此處實現一個簡單的獲取城市資訊的介面。 首先定義介面ApiService,然後實現獲取Observable的方法

public class GetCitiesCase extends UseCase<GetCitiesCase.Api> {
    interface Api {
        @GET("api/china/")
        Observable<List<City>> getCitiesCase();
    }


    public Observable<List<City>> getCities() {
        return ApiClient().getCitiesCase()
                .compose(this.<List<City>>normalSchedulers());
    }

}複製程式碼
  • 使用時呼叫
 new GetCitiesCase().getCities()
                .compose(this.<List<City>>bindToLifecycle())
                .subscribe(new BaseSubscriber<List<City>>() {
                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<City> o) {
                        getCitiesTv.setText("");
                        if (o != null && o.size() != 0) {
                            for (City city : o) {
                                getCitiesTv.setText(getCitiesTv.getText().toString() + city.id.intValue() + "  " + city.name + "\n");
                            }
                        }
                    }
                });複製程式碼

呼叫就很簡單了,只需繫結生命週期(防止記憶體洩漏),然後訂閱Subscriber,處理成功或失敗後的返回。

附上github連結,多多Star噢(~ ̄▽ ̄)~

相關文章