11package com.lxj.androidktx.okhttp
22
3- import com.lxj.androidktx.okhttp.cookie.PersistentCookieStore
43import com.lxj.androidktx.okhttp.progressmanager.ProgressManager
54import com.lxj.androidktx.util.HttpsUtils
65import okhttp3.*
@@ -21,36 +20,35 @@ object OkExt {
2120 val requestCache = hashMapOf<Any , Call >()
2221 val baseUrlMap = hashMapOf<Any , String >() // 存储多个baseUrl, key使用tag来存储
2322 val logInterceptor = HttpLogInterceptor ()
24- var okHttpClient: OkHttpClient = ProgressManager .getInstance().with (OkHttpClient .Builder ()
25- .retryOnConnectionFailure(true )
26- .writeTimeout(httpTimeout, TimeUnit .MILLISECONDS )
27- .readTimeout(httpTimeout, TimeUnit .MILLISECONDS )
28- .connectTimeout(httpTimeout, TimeUnit .MILLISECONDS )
29- .addNetworkInterceptor(logInterceptor)
30- // .cookieJar(PersistentCookieStore())
31- .sslSocketFactory(HttpsUtils .getSslSocketFactory().sSLSocketFactory,
32- HttpsUtils .getSslSocketFactory().trustManager)).build()
23+ var okHttpClient: OkHttpClient = defClientBuilder().build()
3324 var dateFormat: String = " yyyy-MM-dd HH:mm:ss"
3425 var lenientJson: Boolean = false
3526 var globalFailHandler: ((e: Exception ? )-> Unit )? = null
3627 // 是否是成功的响应码
3728 var isSuccessResponse: ((code: Int )-> Boolean )? = null
3829
39- init {
40- // okHttpClient = ProgressManager.getInstance().with(okHttpClient.newBuilder()).build()
41- }
4230
4331// /**
4432// * 自定义超时时间
4533// */
46- // fun timeout(timeout: Long): OkExt{
47- // val builder = okHttpClient.newBuilder()
48- // .writeTimeout(timeout, TimeUnit.MILLISECONDS)
49- // .readTimeout(timeout, TimeUnit.MILLISECONDS)
50- // .connectTimeout(timeout, TimeUnit.MILLISECONDS)
51- // okHttpClient = builder.build()
52- // return this
53- // }
34+ fun timeout (timeout : Long ): OkExt {
35+ val builder = defClientBuilder()
36+ builder.writeTimeout(timeout, TimeUnit .MILLISECONDS )
37+ .readTimeout(timeout, TimeUnit .MILLISECONDS )
38+ .connectTimeout(timeout, TimeUnit .MILLISECONDS )
39+ okHttpClient = builder.build()
40+ return this
41+ }
42+
43+ fun defClientBuilder () = ProgressManager .getInstance().with (OkHttpClient .Builder ()
44+ .retryOnConnectionFailure(true )
45+ .writeTimeout(httpTimeout, TimeUnit .MILLISECONDS )
46+ .readTimeout(httpTimeout, TimeUnit .MILLISECONDS )
47+ .connectTimeout(httpTimeout, TimeUnit .MILLISECONDS )
48+ .addNetworkInterceptor(logInterceptor)
49+ // .cookieJar(PersistentCookieStore())
50+ .sslSocketFactory(HttpsUtils .getSslSocketFactory().sSLSocketFactory,
51+ HttpsUtils .getSslSocketFactory().trustManager))
5452
5553 /* *
5654 * 设置全局公共Header
@@ -75,7 +73,9 @@ object OkExt {
7573 * 设置拦截器
7674 */
7775 fun interceptors (vararg interceptors : Interceptor ): OkExt {
78- okHttpClient.interceptors().addAll(interceptors)
76+ val builder = defClientBuilder()
77+ interceptors.forEach { builder.addInterceptor(it) }
78+ okHttpClient = builder.build()
7979 return this
8080 }
8181
0 commit comments