diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt b/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt index 8e4f1fbec..709aa2877 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt @@ -18,7 +18,7 @@ import java.util.concurrent.Executors val executor: ExecutorService by lazy { Executors.newCachedThreadPool() } -val cronetEngine: ExperimentalCronetEngine by lazy { +val cronetEngine: ExperimentalCronetEngine? by lazy { if (AppConfig.isGooglePlay) { CronetProviderInstaller.installProvider(appCtx) } else { @@ -35,18 +35,22 @@ val cronetEngine: ExperimentalCronetEngine by lazy { enablePublicKeyPinningBypassForLocalTrustAnchors(true) enableBrotli(true)//Brotli压缩 } - val engine = builder.build() - Timber.d("Cronet Version:" + engine.versionString) - //这会导致Jsoup的网络请求出现问题,暂时不接管系统URL - //URL.setURLStreamHandlerFactory(CronetURLStreamHandlerFactory(engine)) - return@lazy engine + try { + val engine = builder.build() + Timber.d("Cronet Version:" + engine.versionString) + //这会导致Jsoup的网络请求出现问题,暂时不接管系统URL + //URL.setURLStreamHandlerFactory(CronetURLStreamHandlerFactory(engine)) + return@lazy engine + } catch (e: Exception) { + return@lazy null + } } -fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest { +fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest? { val url = request.url.toString() val headers: Headers = request.headers val requestBody = request.body - return cronetEngine.newUrlRequestBuilder(url, callback, executor).apply { + return cronetEngine?.newUrlRequestBuilder(url, callback, executor)?.apply { setHttpMethod(request.method)//设置 allowDirectExecutor() headers.forEachIndexed { index, _ -> @@ -68,7 +72,7 @@ fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest { } - }.build() + }?.build() } diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt b/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt index de578c755..41b631c6e 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt +++ b/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt @@ -23,13 +23,16 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor { builder.header("Cookie", cookieStr) } val new = builder.build() - val response: Response = proceedWithCronet(new, chain.call()) - //从Response 中保存Cookie到CookieJar - cookieJar?.saveFromResponse(new.url, Cookie.parseAll(new.url, response.headers)) - response + proceedWithCronet(new, chain.call())?.let { response -> + //从Response 中保存Cookie到CookieJar + cookieJar?.saveFromResponse(new.url, Cookie.parseAll(new.url, response.headers)) + response + } ?: chain.proceed(original) } catch (e: Exception) { //遇到Cronet处理有问题时的情况,如证书过期等等,回退到okhttp处理 - if (e.message.toString().contains("ERR_CERT_", true)||e.message.toString().contains("ERR_SSL_",true)) { + if (e.message.toString().contains("ERR_CERT_", true) + || e.message.toString().contains("ERR_SSL_", true) + ) { chain.proceed(original) } else { throw e @@ -41,12 +44,13 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor { } @Throws(IOException::class) - private fun proceedWithCronet(request: Request, call: Call): Response { - + private fun proceedWithCronet(request: Request, call: Call): Response? { val callback = CronetRequestCallback(request, call) - val urlRequest = buildRequest(request, callback) - urlRequest.start() - return callback.waitForDone(urlRequest) + buildRequest(request, callback)?.let { + it.start() + return callback.waitForDone(it) + } + return null } private fun getCookie(url: HttpUrl): String {