From 3e7ce89959269b71918861e65b1e5ee9adc31755 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Wed, 28 Jun 2023 11:59:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/http/HttpHelper.kt | 16 +++++++++++++- .../help/http/OkHttpExceptionInterceptor.kt | 21 +++++++++++++++++++ .../http/OkhttpUncaughtExceptionHandler.kt | 11 ++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/io/legado/app/help/http/OkHttpExceptionInterceptor.kt create mode 100644 app/src/main/java/io/legado/app/help/http/OkhttpUncaughtExceptionHandler.kt diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 8dd9034a5..277be3952 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -9,6 +9,8 @@ import okhttp3.* import java.net.InetSocketAddress import java.net.Proxy import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.ThreadFactory +import java.util.concurrent.ThreadPoolExecutor import java.util.concurrent.TimeUnit private val proxyClientCache: ConcurrentHashMap by lazy { @@ -56,6 +58,7 @@ val okHttpClient: OkHttpClient by lazy { .connectionSpecs(specs) .followRedirects(true) .followSslRedirects(true) + .addInterceptor(OkHttpExceptionInterceptor) .addInterceptor(Interceptor { chain -> val request = chain.request() val builder = request.newBuilder() @@ -93,7 +96,18 @@ val okHttpClient: OkHttpClient by lazy { } } } - builder.build() + builder.build().apply { + val okHttpName = + OkHttpClient::class.java.name.removePrefix("okhttp3.").removeSuffix("Client") + val executor = dispatcher.executorService as ThreadPoolExecutor + val threadName = "$okHttpName Dispatcher" + executor.threadFactory = ThreadFactory { runnable -> + Thread(runnable, threadName).apply { + isDaemon = false + uncaughtExceptionHandler = OkhttpUncaughtExceptionHandler + } + } + } } /** diff --git a/app/src/main/java/io/legado/app/help/http/OkHttpExceptionInterceptor.kt b/app/src/main/java/io/legado/app/help/http/OkHttpExceptionInterceptor.kt new file mode 100644 index 000000000..c1111a77d --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/OkHttpExceptionInterceptor.kt @@ -0,0 +1,21 @@ +package io.legado.app.help.http + +import io.legado.app.utils.asIOException +import okhttp3.Interceptor +import okhttp3.Response +import java.io.IOException + +object OkHttpExceptionInterceptor : Interceptor { + + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response { + try { + return chain.proceed(chain.request()) + } catch (e: IOException) { + throw e + } catch (e: Throwable) { + throw IOException(e) + } + } + +} diff --git a/app/src/main/java/io/legado/app/help/http/OkhttpUncaughtExceptionHandler.kt b/app/src/main/java/io/legado/app/help/http/OkhttpUncaughtExceptionHandler.kt new file mode 100644 index 000000000..69b526ea9 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/OkhttpUncaughtExceptionHandler.kt @@ -0,0 +1,11 @@ +package io.legado.app.help.http + +import io.legado.app.constant.AppLog + +object OkhttpUncaughtExceptionHandler : Thread.UncaughtExceptionHandler { + + override fun uncaughtException(t: Thread, e: Throwable) { + AppLog.put("Okhttp Dispatcher中的线程执行出错\n${e.localizedMessage}", e) + } + +}