修复Cronet onFailed时由于UrlResponseInfo可能为空而方法声明为非空,而导致不能正常返回错误。

pull/1125/head
ag2s20150909 3 years ago
parent a793177a93
commit 8367bc2046
  1. 2
      app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt
  2. 32
      app/src/main/java/io/legado/app/help/http/cronet/CronetUrlRequestCallback.kt

@ -40,7 +40,7 @@ fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest {
requestBuilder.addHeader("Cookie", cookie) requestBuilder.addHeader("Cookie", cookie)
} }
val headers: Headers = request.headers val headers: Headers = request.headers
headers.forEachIndexed { index, pair -> headers.forEachIndexed { index, _ ->
requestBuilder.addHeader(headers.name(index), headers.value(index)) requestBuilder.addHeader(headers.name(index), headers.value(index))
} }

@ -22,6 +22,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
eventListener: EventListener? = null, eventListener: EventListener? = null,
responseCallback: Callback? = null responseCallback: Callback? = null
) : UrlRequest.Callback() { ) : UrlRequest.Callback() {
private val eventListener: EventListener? private val eventListener: EventListener?
private val responseCallback: Callback? private val responseCallback: Callback?
private var followCount = 0 private var followCount = 0
@ -37,7 +38,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
if (mException != null) { if (mException != null) {
throw mException as IOException throw mException as IOException
} }
return mResponse return this.mResponse
} }
override fun onRedirectReceived( override fun onRedirectReceived(
@ -62,7 +63,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
} }
override fun onResponseStarted(request: UrlRequest, info: UrlResponseInfo) { 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") // val sb: StringBuilder = StringBuilder(info.url).append("\r\n")
// sb.append("[Cached:").append(info.wasCached()).append("][StatusCode:") // sb.append("[Cached:").append(info.wasCached()).append("][StatusCode:")
// .append(info.httpStatusCode).append("][StatusText:").append(info.httpStatusText) // .append(info.httpStatusCode).append("][StatusText:").append(info.httpStatusText)
@ -74,7 +75,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
// } // }
// Log.e("Cronet", sb.toString()) // Log.e("Cronet", sb.toString())
if (eventListener != null) { if (eventListener != null) {
eventListener.responseHeadersEnd(mCall, mResponse) eventListener.responseHeadersEnd(mCall, this.mResponse)
eventListener.responseBodyStart(mCall) eventListener.responseBodyStart(mCall)
} }
request.read(ByteBuffer.allocateDirect(32 * 1024)) request.read(ByteBuffer.allocateDirect(32 * 1024))
@ -99,34 +100,43 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) { override fun onSucceeded(request: UrlRequest, info: UrlResponseInfo) {
eventListener?.responseBodyEnd(mCall, info.receivedByteCount) 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() ?: "text/plain; charset=\"utf-8\"").toMediaTypeOrNull()
val responseBody: ResponseBody = val responseBody: ResponseBody =
mBytesReceived.toByteArray().toResponseBody(contentType) mBytesReceived.toByteArray().toResponseBody(contentType)
val newRequest = originalRequest.newBuilder().url(info.url).build() 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() mResponseCondition.open()
eventListener?.callEnd(mCall) eventListener?.callEnd(mCall)
if (responseCallback != null) { if (responseCallback != null) {
try { try {
responseCallback.onResponse(mCall, mResponse) responseCallback.onResponse(mCall, this.mResponse)
} catch (e: IOException) { } catch (e: IOException) {
// Pass? // Pass?
} }
} }
} }
override fun onFailed(request: UrlRequest, info: UrlResponseInfo, error: CronetException) { //UrlResponseInfo可能为null
val e = IOException("Cronet Exception Occurred", error) 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 mException = e
mResponseCondition.open() mResponseCondition.open()
eventListener?.callFailed(mCall, e)
this.eventListener?.callFailed(mCall, e)
responseCallback?.onFailure(mCall, e) responseCallback?.onFailure(mCall, e)
} }
override fun onCanceled(request: UrlRequest, info: UrlResponseInfo) { override fun onCanceled(request: UrlRequest, info: UrlResponseInfo) {
mResponseCondition.open() mResponseCondition.open()
eventListener?.callEnd(mCall)
this.eventListener?.callEnd(mCall)
} }
companion object { companion object {
@ -194,7 +204,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
} }
init { init {
mResponse = Response.Builder() this.mResponse = Response.Builder()
.sentRequestAtMillis(System.currentTimeMillis()) .sentRequestAtMillis(System.currentTimeMillis())
.request(originalRequest) .request(originalRequest)
.protocol(Protocol.HTTP_1_0) .protocol(Protocol.HTTP_1_0)

Loading…
Cancel
Save