Linjiajia
2023-12-29 590c1cff46b105d774271f950caa9f65523f05c1
app_base/src/main/java/com/android/app_base/http/RetrofitManager.java
@@ -2,9 +2,17 @@
import androidx.collection.CircularArray;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
@@ -17,13 +25,17 @@
 */
public class RetrofitManager {
    private static RetrofitManager instance;
    private static volatile RetrofitManager instance;
    private final Map<String,Retrofit> retrofitMap;
    private OkHttpClient mClient;
    public static RetrofitManager getInstance(){
        if (instance == null){
            instance = new RetrofitManager();
        if (instance == null) {
            synchronized (RetrofitManager.class) {
                if (instance == null) {
                    instance = new RetrofitManager();
                }
            }
        }
        return instance;
    }
@@ -37,11 +49,16 @@
     * 获取Retrofit对象
     */
    public Retrofit getRetrofit(String baseUrl) {
        Gson gson = new GsonBuilder()
                //配置你的Gson
                .setDateFormat("yyyy-MM-dd hh:mm:ss")
                .registerTypeHierarchyAdapter(String.class,STRING)//设置解析的时候null转成""
                .create();
        Retrofit retrofit = retrofitMap.get(baseUrl);
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .client(mClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .baseUrl(baseUrl)
                    .build();
@@ -49,12 +66,42 @@
        }
        return retrofit;
    }
    /**
     * 自定义TypeAdapter ,null对象将被解析成空字符串
     */
    public static final TypeAdapter<String> STRING = new TypeAdapter<String>() {
        public String read(JsonReader reader) {
            try {
                if (reader.peek() == JsonToken.NULL) {
                    reader.nextNull();
                    return ""; // 原先是返回null,这里改为返回空字符串
                }
                return reader.nextString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }
        public void write(JsonWriter writer, String value) {
            try {
                if (value == null) {
                    writer.nullValue();
                    return;
                }
                writer.value(value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    /**
     * 设置 OkHttpClient
     * 设置 自定义OkHttpClient
     */
    public RetrofitManager setOkHttpClient(OkHttpClient client) {
        this.mClient = client;
        return instance;
    }
}