diff --git a/http/build.gradle.kts b/http/build.gradle.kts index ca850ba..455be69 100644 --- a/http/build.gradle.kts +++ b/http/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "cn.numeron" -version = "1.0.8" +version = "1.0.9" tasks.withType { kotlinOptions { diff --git a/http/src/main/kotlin/cn/numeron/okhttp/RetryInterceptor.kt b/http/src/main/kotlin/cn/numeron/okhttp/RetryInterceptor.kt index 011d56f..642ecbf 100644 --- a/http/src/main/kotlin/cn/numeron/okhttp/RetryInterceptor.kt +++ b/http/src/main/kotlin/cn/numeron/okhttp/RetryInterceptor.kt @@ -1,6 +1,7 @@ package cn.numeron.okhttp import okhttp3.Interceptor +import okhttp3.Request import okhttp3.Response import java.io.IOException @@ -10,10 +11,23 @@ class RetryInterceptor(private val retryCount: Int = 2) : Interceptor { return try { chain.proceed(chain.request()) } catch (exception: IOException) { - retry(0, exception, chain) + if (isAllowRetry(chain.request())) { + retry(0, exception, chain) + } else throw exception } } + /** 是否是GET请求 */ + private fun isAllowRetry(request: Request): Boolean { + if (request.method == "GET") { + return true + } + return request.url.pathSegments.any(::isGetUrl) + } + + /** 是否是获取数据的url */ + private fun isGetUrl(segment: String) = segment.startsWith("get") + private fun retry(count: Int, exception: IOException, chain: Interceptor.Chain): Response { return if (count < retryCount) { try {