From ab079cf9a5631cfeab6b22e55b01a27ea754ad5c Mon Sep 17 00:00:00 2001 From: Ztiany Date: Fri, 11 Oct 2019 16:18:20 +0800 Subject: [PATCH] update lib-network --- .../java/com/android/sdk/net/gson/AutoGson.java | 1 + .../gson/ErrorJsonLenientConverterFactory.java | 14 +++++++------- .../android/sdk/net/gson/JsonDeserializers.java | 8 ++++---- .../android/sdk/net/provider/HttpConfig.java | 17 ++++++++++++----- .../android/sdk/net/service/ServiceFactory.java | 3 +-- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib_network/src/main/java/com/android/sdk/net/gson/AutoGson.java b/lib_network/src/main/java/com/android/sdk/net/gson/AutoGson.java index dc91002..6ab8a8c 100644 --- a/lib_network/src/main/java/com/android/sdk/net/gson/AutoGson.java +++ b/lib_network/src/main/java/com/android/sdk/net/gson/AutoGson.java @@ -26,4 +26,5 @@ public @interface AutoGson { * @return the annotated class's real type. */ Class autoClass(); + } diff --git a/lib_network/src/main/java/com/android/sdk/net/gson/ErrorJsonLenientConverterFactory.java b/lib_network/src/main/java/com/android/sdk/net/gson/ErrorJsonLenientConverterFactory.java index b641b0e..dfaf710 100644 --- a/lib_network/src/main/java/com/android/sdk/net/gson/ErrorJsonLenientConverterFactory.java +++ b/lib_network/src/main/java/com/android/sdk/net/gson/ErrorJsonLenientConverterFactory.java @@ -10,7 +10,6 @@ import okhttp3.RequestBody; import okhttp3.ResponseBody; import retrofit2.Converter; import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; import timber.log.Timber; /** @@ -21,9 +20,9 @@ import timber.log.Timber; */ public class ErrorJsonLenientConverterFactory extends Converter.Factory { - private final GsonConverterFactory mGsonConverterFactory; + private final Converter.Factory mGsonConverterFactory; - public ErrorJsonLenientConverterFactory(GsonConverterFactory gsonConverterFactory) { + public ErrorJsonLenientConverterFactory(Converter.Factory gsonConverterFactory) { mGsonConverterFactory = gsonConverterFactory; } @@ -32,22 +31,23 @@ public class ErrorJsonLenientConverterFactory extends Converter.Factory { Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { + return mGsonConverterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); } @Override - public Converter responseBodyConverter(Type type, - Annotation[] annotations, - Retrofit retrofit) { + public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { final Converter delegateConverter = mGsonConverterFactory.responseBodyConverter(type, annotations, retrofit); + assert delegateConverter != null; return (Converter) value -> { try { return delegateConverter.convert(value); } catch (Exception e/*防止闪退:JsonSyntaxException、IOException or MalformedJsonException*/) { Timber.e(e, "Json covert error -->error "); - return NetContext.get().netProvider().errorDataAdapter().createErrorDataStub(type, annotations, retrofit, value);//服务器数据格式错误 + //服务器数据格式错误 + return NetContext.get().netProvider().errorDataAdapter().createErrorDataStub(type, annotations, retrofit, value); } }; diff --git a/lib_network/src/main/java/com/android/sdk/net/gson/JsonDeserializers.java b/lib_network/src/main/java/com/android/sdk/net/gson/JsonDeserializers.java index abfdd21..aaed985 100644 --- a/lib_network/src/main/java/com/android/sdk/net/gson/JsonDeserializers.java +++ b/lib_network/src/main/java/com/android/sdk/net/gson/JsonDeserializers.java @@ -24,7 +24,7 @@ class JsonDeserializers { try { return json.getAsDouble(); } catch (Exception e) { - Timber.e(e, "DoubleJsonDeserializer-deserialize-error:" + (json != null ? json.toString() : "")); + Timber.e(e, "DoubleJsonDeserializer-deserialize-error:%s", (json != null ? json.toString() : "")); return 0D; } } @@ -38,7 +38,7 @@ class JsonDeserializers { try { return json.getAsInt(); } catch (Exception e) { - Timber.e(e, "IntegerJsonDeserializer-deserialize-error:" + (json != null ? json.toString() : "")); + Timber.e(e, "IntegerJsonDeserializer-deserialize-error:%s", (json != null ? json.toString() : "")); return 0; } } @@ -52,7 +52,7 @@ class JsonDeserializers { try { return json.getAsString(); } catch (Exception e) { - Timber.e(e, "StringJsonDeserializer-deserialize-error:" + (json != null ? json.toString() : "")); + Timber.e(e, "StringJsonDeserializer-deserialize-error:%s", (json != null ? json.toString() : "")); return null; } } @@ -74,7 +74,7 @@ class JsonDeserializers { try { return json.getAsFloat(); } catch (Exception e) { - Timber.e(e, "FloatJsonDeserializer-deserialize-error:" + (json != null ? json.toString() : "")); + Timber.e(e, "FloatJsonDeserializer-deserialize-error:%s", (json != null ? json.toString() : "")); return 0F; } } diff --git a/lib_network/src/main/java/com/android/sdk/net/provider/HttpConfig.java b/lib_network/src/main/java/com/android/sdk/net/provider/HttpConfig.java index a292d8f..8b24cae 100644 --- a/lib_network/src/main/java/com/android/sdk/net/provider/HttpConfig.java +++ b/lib_network/src/main/java/com/android/sdk/net/provider/HttpConfig.java @@ -1,5 +1,6 @@ package com.android.sdk.net.provider; +import androidx.annotation.NonNull; import io.reactivex.schedulers.Schedulers; import okhttp3.OkHttpClient; import retrofit2.Retrofit; @@ -11,15 +12,21 @@ import retrofit2.Retrofit; */ public interface HttpConfig { - void configHttp(OkHttpClient.Builder builder); + /** + * base url for build retrofit. + */ + String baseUrl(); + + /** + * config OkHttp client. + */ + void configHttp(@NonNull OkHttpClient.Builder builder); /** * default config is {@link retrofit2.converter.gson.GsonConverterFactory}、{@link retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory} with {@link Schedulers#io()} * - * @return if true, default config do nothing. + * @return if true, default config do nothing, and all config up to you. */ - boolean configRetrofit(Retrofit.Builder builder); - - String baseUrl(); + boolean configRetrofit(@NonNull OkHttpClient okHttpClient, @NonNull Retrofit.Builder builder); } diff --git a/lib_network/src/main/java/com/android/sdk/net/service/ServiceFactory.java b/lib_network/src/main/java/com/android/sdk/net/service/ServiceFactory.java index bea094b..85b1a64 100644 --- a/lib_network/src/main/java/com/android/sdk/net/service/ServiceFactory.java +++ b/lib_network/src/main/java/com/android/sdk/net/service/ServiceFactory.java @@ -29,9 +29,8 @@ public class ServiceFactory { mBaseUrl = httpConfig.baseUrl(); Retrofit.Builder builder = new Retrofit.Builder(); - boolean abort = httpConfig.configRetrofit(builder); - if (!abort) { + if (!httpConfig.configRetrofit(mOkHttpClient, builder)) { builder.baseUrl(mBaseUrl) .client(okHttpClient) .addConverterFactory(new ErrorJsonLenientConverterFactory(GsonConverterFactory.create(GsonUtils.gson())))