新增服务端返回 code 1415 全局拦截跳转至完善资料页功能
(cherry picked from commit c09c07bb01dff140dfebb079fb9b8f6b18468872)
This commit is contained in:
@@ -40,7 +40,6 @@ dependencies {
|
||||
def okhttp3 = "3.14.9"
|
||||
def okio = "2.2.2"
|
||||
def rxjava_adapter = "2.3.0"
|
||||
def gson_converter = "2.9.0"
|
||||
def rxjava = "2.1.7"
|
||||
def rxjava_android = "2.0.1"
|
||||
def rxlifecycle = "3.1.0"
|
||||
@@ -73,8 +72,7 @@ dependencies {
|
||||
api "com.squareup.okhttp3:okhttp:${okhttp3}"
|
||||
api "com.squareup.okhttp3:logging-interceptor:${okhttp3}"
|
||||
api "com.squareup.retrofit2:adapter-rxjava2:${rxjava_adapter}"
|
||||
api "com.squareup.retrofit2:converter-gson:${gson_converter}"
|
||||
|
||||
api 'com.google.code.gson:gson:2.8.7'
|
||||
api "com.squareup.okio:okio:${okio}"
|
||||
|
||||
api "com.scwang.smartrefresh:SmartRefreshLayout:${SmartRefreshLayoutVersion}"
|
||||
|
@@ -1,19 +0,0 @@
|
||||
package com.yizhuan.xchat_android_library.net;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLSession;
|
||||
|
||||
/**
|
||||
* <p> </p>
|
||||
*
|
||||
* @author jiahui
|
||||
* @date 2017/12/16
|
||||
*/
|
||||
public final class ErBanAllHostnameVerifier implements HostnameVerifier {
|
||||
|
||||
@Override
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
//信任所有的证书
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Square, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.yizhuan.xchat_android_library.net.rxnet.converter;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Converter;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
/**
|
||||
* A {@linkplain Converter.Factory converter} which uses Gson for JSON.
|
||||
*
|
||||
* <p>Because Gson is so flexible in the types it supports, this converter assumes that it can
|
||||
* handle all types. If you are mixing JSON serialization with something else (such as protocol
|
||||
* buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this
|
||||
* instance} last to allow the other converters a chance to see their types.
|
||||
*/
|
||||
public final class GsonConverterFactory extends Converter.Factory {
|
||||
/**
|
||||
* Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
|
||||
* decoding from JSON (when no charset is specified by a header) will use UTF-8.
|
||||
*/
|
||||
public static GsonConverterFactory create() {
|
||||
return create(new Gson());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance using {@code gson} for conversion. Encoding to JSON and decoding from JSON
|
||||
* (when no charset is specified by a header) will use UTF-8.
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
|
||||
public static GsonConverterFactory create(Gson gson) {
|
||||
if (gson == null) throw new NullPointerException("gson == null");
|
||||
return new GsonConverterFactory(gson);
|
||||
}
|
||||
|
||||
private final Gson gson;
|
||||
|
||||
private GsonConverterFactory(Gson gson) {
|
||||
this.gson = gson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<ResponseBody, ?> responseBodyConverter(
|
||||
Type type, Annotation[] annotations, Retrofit retrofit) {
|
||||
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
|
||||
return new GsonResponseBodyConverter<>(gson, adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Converter<?, RequestBody> requestBodyConverter(
|
||||
Type type,
|
||||
Annotation[] parameterAnnotations,
|
||||
Annotation[] methodAnnotations,
|
||||
Retrofit retrofit) {
|
||||
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
|
||||
return new GsonRequestBodyConverter<>(gson, adapter);
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package com.yizhuan.xchat_android_library.net.rxnet.converter;
|
||||
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
|
||||
import kotlin.jvm.functions.Function1;
|
||||
|
||||
public class GsonConverterPlugins {
|
||||
|
||||
private static Function1<Object, Object> resultHandler;
|
||||
static volatile boolean lockdown;
|
||||
|
||||
public static void onConvertResult(@Nullable Object object) {
|
||||
if (resultHandler != null && object != null) {
|
||||
resultHandler.invoke(object);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setResultHandler(Function1<Object, Object> handler) {
|
||||
if (lockdown) {
|
||||
throw new IllegalStateException("GsonConverterPlugins can't be changed anymore");
|
||||
}
|
||||
resultHandler = handler;
|
||||
}
|
||||
|
||||
public static void lockdown() {
|
||||
lockdown = true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Square, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.yizhuan.xchat_android_library.net.rxnet.converter;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import okio.Buffer;
|
||||
import retrofit2.Converter;
|
||||
|
||||
final class GsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
|
||||
private static final MediaType MEDIA_TYPE = MediaType.get("application/json; charset=UTF-8");
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private final Gson gson;
|
||||
private final TypeAdapter<T> adapter;
|
||||
|
||||
GsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
|
||||
this.gson = gson;
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestBody convert(@NotNull T value) throws IOException {
|
||||
Buffer buffer = new Buffer();
|
||||
Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
|
||||
JsonWriter jsonWriter = gson.newJsonWriter(writer);
|
||||
adapter.write(jsonWriter, value);
|
||||
jsonWriter.close();
|
||||
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Square, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.yizhuan.xchat_android_library.net.rxnet.converter;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonIOException;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.Converter;
|
||||
|
||||
final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
|
||||
private final Gson gson;
|
||||
private final TypeAdapter<T> adapter;
|
||||
|
||||
GsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
|
||||
this.gson = gson;
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T convert(ResponseBody value) throws IOException {
|
||||
JsonReader jsonReader = gson.newJsonReader(value.charStream());
|
||||
try {
|
||||
T result = adapter.read(jsonReader);
|
||||
if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
|
||||
throw new JsonIOException("JSON document was not fully consumed.");
|
||||
}
|
||||
//hook
|
||||
GsonConverterPlugins.onConvertResult(result);
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
value.close();
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ import android.content.Context;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.yizhuan.xchat_android_library.BuildConfig;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.converter.GsonConverterFactory;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.https.HttpsUtils;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.interceptor.HttpLoggingInterceptor;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.utils.RxNetLog;
|
||||
@@ -29,7 +30,6 @@ import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
/**
|
||||
* <p> RxNet 管理类 </p>
|
||||
|
Reference in New Issue
Block a user