From 8367bc2046e6bf123b23bb08ff4e0bb0b5056cd6 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Sat, 17 Jul 2021 15:50:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DCronet=20onFailed=E6=97=B6?= =?UTF-8?q?=E7=94=B1=E4=BA=8EUrlResponseInfo=E5=8F=AF=E8=83=BD=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E8=80=8C=E6=96=B9=E6=B3=95=E5=A3=B0=E6=98=8E=E4=B8=BA?= =?UTF-8?q?=E9=9D=9E=E7=A9=BA=EF=BC=8C=E8=80=8C=E5=AF=BC=E8=87=B4=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E6=AD=A3=E5=B8=B8=E8=BF=94=E5=9B=9E=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/help/http/cronet/CronetHelper.kt | 2 +- .../http/cronet/CronetUrlRequestCallback.kt | 32 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) 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 b728beb63..8229f46cd 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 @@ -40,7 +40,7 @@ fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest { requestBuilder.addHeader("Cookie", cookie) } val headers: Headers = request.headers - headers.forEachIndexed { index, pair -> + headers.forEachIndexed { index, _ -> requestBuilder.addHeader(headers.name(index), headers.value(index)) } diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetUrlRequestCallback.kt b/app/src/main/java/io/legado/app/help/http/cronet/CronetUrlRequestCallback.kt index e7e3686c0..0e59ed7de 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetUrlRequestCallback.kt +++ b/app/src/main/java/io/legado/app/help/http/cronet/CronetUrlRequestCallback.kt @@ -22,6 +22,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor( eventListener: EventListener? = null, responseCallback: Callback? = null ) : UrlRequest.Callback() { + private val eventListener: EventListener? private val responseCallback: Callback? private var followCount = 0 @@ -37,7 +38,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor( if (mException != null) { throw mException as IOException } - return mResponse + return this.mResponse } override fun onRedirectReceived( @@ -62,7 +63,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor( } override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) { - mResponse = responseFromResponse(mResponse, info) + this.mResponse = responseFromResponse(this.mResponse, info) // val sb: StringBuilder = StringBuilder(info.url).append("\r\n") // sb.append("[Cached:").append(info.wasCached()).append("][StatusCode:") // .append(info.httpStatusCode).append("][StatusText:").append(info.httpStatusText) @@ -74,7 +75,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor( // } // Log.e("Cronet", sb.toString()) if (eventListener != null) { - eventListener.responseHeadersEnd(mCall, mResponse) + eventListener.responseHeadersEnd(mCall, this.mResponse) eventListener.responseBodyStart(mCall) } request.read(ByteBuffer.allocateDirect(32 * 1024)) @@ -99,34 +100,43 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor( override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) { eventListener?.responseBodyEnd(mCall, info.receivedByteCount) - val contentType: MediaType? = (mResponse.header("content-type") + val contentType: MediaType? = (this.mResponse.header("content-type") ?: "text/plain; charset=\"utf-8\"").toMediaTypeOrNull() val responseBody: ResponseBody = mBytesReceived.toByteArray().toResponseBody(contentType) val newRequest = originalRequest.newBuilder().url(info.url).build() - mResponse = mResponse.newBuilder().body(responseBody).request(newRequest).build() + this.mResponse = this.mResponse.newBuilder().body(responseBody).request(newRequest).build() mResponseCondition.open() eventListener?.callEnd(mCall) if (responseCallback != null) { try { - responseCallback.onResponse(mCall, mResponse) + responseCallback.onResponse(mCall, this.mResponse) } catch (e: IOException) { // Pass? } } } - override fun onFailed(request: UrlRequest, info: UrlResponseInfo, error: CronetException) { - val e = IOException("Cronet Exception Occurred", error) + //UrlResponseInfo可能为null + override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) { + Log.e(TAG, error.message.toString()) + val msg = error.localizedMessage + val e = IOException(msg?.substring(msg.indexOf("net::")), error) mException = e mResponseCondition.open() - eventListener?.callFailed(mCall, e) + + this.eventListener?.callFailed(mCall, e) + + responseCallback?.onFailure(mCall, e) } override fun onCanceled(request: UrlRequest, info: UrlResponseInfo) { mResponseCondition.open() - eventListener?.callEnd(mCall) + + this.eventListener?.callEnd(mCall) + + } companion object { @@ -194,7 +204,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor( } init { - mResponse = Response.Builder() + this.mResponse = Response.Builder() .sentRequestAtMillis(System.currentTimeMillis()) .request(originalRequest) .protocol(Protocol.HTTP_1_0)