Administrator
2021-11-09 5f50bd6ea5d5bdb7b8ea4d9e9a5851067b9aec1b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.duqing.missions.retrofit;
 
 
import com.duqing.missions.BuildConfig;
import com.duqing.missions.retrofit.Interceptor.EncryptInterceptor;
import com.duqing.missions.retrofit.Interceptor.HttpLoggingInterceptor;
import com.duqing.missions.retrofit.api.CommonApiCenter;
import com.duqing.missions.retrofit.converter.DecryptGsonConverterFactory;
 
import java.util.Collections;
import java.util.concurrent.TimeUnit;
 
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
 
/**
 * My father is Object, ites purpose of
 *
 * @purpose Created by Runt (qingingrunt2010@qq.com) on 2021-7-9.
 */
 
public class RetrofitUtils {
    public static String HOST_IP_ADDR;
    static RetrofitUtils instance;
    Retrofit retrofit/*log输出,驼峰转换*/,unHumpRetrofit/*log输出,不强制驼峰转换*/,
            unLogRetrofit/*log不输出,驼峰转换*/,unLogHumpRetorfit/*log不输出,不强制驼峰转换*/;
    CommonApiCenter commonApi;//常用接口
 
    OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .addInterceptor(new EncryptInterceptor());
    OkHttpClient.Builder logBuilder = new OkHttpClient.Builder()
            .addInterceptor(new HttpLoggingInterceptor());//log打印拦截器
 
    public static RetrofitUtils getInstance() {
        if(instance == null){
            instance = new RetrofitUtils();
        }
        return instance;
    }
 
    /**
     * log输出,gson驼峰转换
     * @return
     */
    public <T> T getRetrofit(Class<T> clas) {
        if(retrofit == null){
            retrofit = getRetrofit(getOkHttpClient(logBuilder),
                    new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create(true))) ;
        }
        if(!BuildConfig.DEBUG){//正式版 不打印log
            return getUnLogRetrofit(clas);
        }
        return retrofit.create(clas);
    }
 
    /**
     * log输出,gson不转换驼峰
     * @return
     */
    public <T> T getUnHumpRetrofit(Class<T> clas) {
        if(unHumpRetrofit == null){
            unHumpRetrofit = getRetrofit(getOkHttpClient(logBuilder),
                    new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create())) ;
        }
        if(!BuildConfig.DEBUG){//正式版 不打印log
            return getUnLogHumpRetorfit(clas);
        }
        return unHumpRetrofit.create(clas);
    }
 
    /**
     * log不输出,gson驼峰转换
     * @return
     */
    public <T> T  getUnLogRetrofit(Class<T> clas) {
        if(unLogRetrofit == null){
            unLogRetrofit = getRetrofit(getOkHttpClient(builder),
                    new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create(true))) ;
        }
        return unLogRetrofit.create(clas);
    }
 
    /**
     * log不输出,gson不转换驼峰
     * @return
     */
    public <T> T getUnLogHumpRetorfit(Class<T> clas) {
        if(unLogHumpRetorfit == null){
            unLogHumpRetorfit = getRetrofit(getOkHttpClient(builder),
                    new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create())) ;
        }
        return unLogHumpRetorfit.create(clas);
    }
 
    private OkHttpClient getOkHttpClient(OkHttpClient.Builder builder){
        return builder.connectTimeout(10, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间
                .protocols(Collections.singletonList(Protocol.HTTP_1_1))
                .eventListenerFactory(NetWorkListenear.get())
                .build();
    }
 
    private Retrofit getRetrofit(OkHttpClient client,Retrofit.Builder builder){
        return builder
                //设置OKHttpClient
                .client(client)
                //设置baseUrl,注意,baseUrl必须后缀"/"
                .baseUrl(BuildConfig.ENVIRONMENT.equals("develop")?HOST_IP_ADDR:BuildConfig.HOST_IP_ADDR)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }
 
 
    /**
     * 常用接口
     * @return
     */
    public CommonApiCenter getCommonApi(){
        if(commonApi == null){
            commonApi  = getRetrofit(CommonApiCenter.class);
        }
        return commonApi;
    }
}