update lib-network

androidx
Ztiany 5 years ago
parent 7b3d5711cd
commit ab079cf9a5
  1. 1
      lib_network/src/main/java/com/android/sdk/net/gson/AutoGson.java
  2. 14
      lib_network/src/main/java/com/android/sdk/net/gson/ErrorJsonLenientConverterFactory.java
  3. 8
      lib_network/src/main/java/com/android/sdk/net/gson/JsonDeserializers.java
  4. 17
      lib_network/src/main/java/com/android/sdk/net/provider/HttpConfig.java
  5. 3
      lib_network/src/main/java/com/android/sdk/net/service/ServiceFactory.java

@ -26,4 +26,5 @@ public @interface AutoGson {
* @return the annotated class's real type.
*/
Class autoClass();
}

@ -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<ResponseBody, ?> responseBodyConverter(Type type,
Annotation[] annotations,
Retrofit retrofit) {
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
final Converter<ResponseBody, ?> delegateConverter = mGsonConverterFactory.responseBodyConverter(type, annotations, retrofit);
assert delegateConverter != null;
return (Converter<ResponseBody, Object>) 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);
}
};

@ -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;
}
}

@ -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);
}

@ -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())))

Loading…
Cancel
Save