Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
junix committed Sep 11, 2024
1 parent b1f3f0c commit d957c38
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HttpExtFragment : BaseFragment() {
"version" to "3.2.0",
"token" to "OrE1GPYXyOb0z_w_s1dpq2rsM4t0DjwK_1538967658"
)
OkExt.logInterceptor.addExcludeUrl("baidu.com", "", "")
// OkExt.logInterceptor.addExcludeUrl("baidu.com")

// OkWrapper.headers("header1" to "a", "header2" to "b")
loadingDialog.observeState(this, demoVM.testData, onSuccess = {
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/lxj/androidktx/okhttp/HttpExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.net.URLConnection.getFileNameMap
fun String.http(httpTag: Any = this, baseUrlTag: String = OkExt.DefaultUrlTag): RequestWrapper {
val baseUrl = OkExt.baseUrlMap[baseUrlTag]
if(baseUrlTag!= OkExt.NoBaseUrl && baseUrl==null){
LogUtils.wTag("OkExt","tag为[${baseUrlTag}]的baseUrl不存在,请先调用OkExt.baseUrl(tag, url)方法进行配置")
LogUtils.wTag("OkExt","tag为[${baseUrlTag}]的baseUrl未配置,如需要可调用OkExt.baseUrl(tag, url)方法进行配置")
}
return RequestWrapper(httpTag, url = "${baseUrl ?: ""}${this}")
}
Expand Down
35 changes: 16 additions & 19 deletions library/src/main/java/com/lxj/androidktx/okhttp/OkExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,36 @@ object OkExt {
val requestCache = hashMapOf<Any, Call>()
val baseUrlMap = hashMapOf<Any, String>() //存储多个baseUrl, key使用tag来存储
val logInterceptor = HttpLogInterceptor()
var okHttpClient: OkHttpClient = OkHttpClient.Builder()
var okHttpClient: OkHttpClient = ProgressManager.getInstance().with(OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.writeTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.readTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.connectTimeout(httpTimeout, TimeUnit.MILLISECONDS)
.addNetworkInterceptor(logInterceptor)
// .cookieJar(PersistentCookieStore())
.sslSocketFactory(HttpsUtils.getSslSocketFactory().sSLSocketFactory,
HttpsUtils.getSslSocketFactory().trustManager)
.build()
HttpsUtils.getSslSocketFactory().trustManager)).build()
var dateFormat: String = "yyyy-MM-dd HH:mm:ss"
var lenientJson: Boolean = false
var globalFailHandler: ((e: Exception?)->Unit)? = null
//是否是成功的响应码
var isSuccessResponse: ((code: Int)-> Boolean)? = null

init {
okHttpClient = ProgressManager.getInstance().with(okHttpClient.newBuilder()).build()
// okHttpClient = ProgressManager.getInstance().with(okHttpClient.newBuilder()).build()
}

/**
* 自定义超时时间
*/
fun timeout(timeout: Long): OkExt{
val builder = okHttpClient.newBuilder()
.writeTimeout(timeout, TimeUnit.MILLISECONDS)
.readTimeout(timeout, TimeUnit.MILLISECONDS)
.connectTimeout(timeout, TimeUnit.MILLISECONDS)
okHttpClient = builder.build()
return this
}
// /**
// * 自定义超时时间
// */
// fun timeout(timeout: Long): OkExt{
// val builder = okHttpClient.newBuilder()
// .writeTimeout(timeout, TimeUnit.MILLISECONDS)
// .readTimeout(timeout, TimeUnit.MILLISECONDS)
// .connectTimeout(timeout, TimeUnit.MILLISECONDS)
// okHttpClient = builder.build()
// return this
// }

/**
* 设置全局公共Header
Expand All @@ -76,9 +75,7 @@ object OkExt {
* 设置拦截器
*/
fun interceptors(vararg interceptors: Interceptor): OkExt {
val builder = okHttpClient.newBuilder()
interceptors.forEach { builder.addInterceptor(it) }
okHttpClient = builder.build()
okHttpClient.interceptors().addAll(interceptors)
return this
}

Expand Down Expand Up @@ -141,6 +138,6 @@ object OkExt {

fun plainTextBody(string: String) = RequestBody.create(MediaType.parse("text/plain"), string)

fun jsontBody(string: String) = RequestBody.create(MediaType.parse("application/json"), string)
fun jsonBody(string: String) = RequestBody.create(MediaType.parse("application/json"), string)

}

0 comments on commit d957c38

Please sign in to comment.