Cronet 支持okhttp设置的超时,超时后自动取消请求。

pull/1264/head
ag2s20150909 3 years ago
parent 6957d89965
commit 87a892e974
  1. BIN
      app/cronetlib/cronet_api.jar
  2. BIN
      app/cronetlib/cronet_impl_common_java.jar
  3. BIN
      app/cronetlib/src/cronet_api-src.jar
  4. BIN
      app/cronetlib/src/cronet_impl_common_java-src.jar
  5. 2
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt
  6. 2
      app/src/main/java/io/legado/app/help/http/cronet/CronetLoader.kt
  7. 20
      app/src/main/java/io/legado/app/help/http/cronet/CronetUrlRequestCallback.kt
  8. 2
      app/src/main/java/io/legado/app/ui/dict/DictViewModel.kt
  9. 2
      gradle.properties

Binary file not shown.

@ -44,7 +44,7 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
val callback = CronetUrlRequestCallback(request, call)
val urlRequest = buildRequest(request, callback)
urlRequest.start()
return callback.waitForDone()
return callback.waitForDone(urlRequest)
}
private fun getCookie(url: HttpUrl): String {

@ -40,7 +40,7 @@ object CronetLoader : CronetEngine.Builder.LibraryLoader() {
md5Url = ("https://cdn.jsdelivr.net/gh/ag2s20150909/cronet-repo@" +
ImplVersion.getCronetVersion() + "/cronet/" + ImplVersion.getCronetVersion() + "/"
+ getCpuAbi(appCtx) + "/" + soName + ".js")
val dir = appCtx.getDir("lib", Context.MODE_PRIVATE)
val dir = appCtx.getDir("cronet", Context.MODE_PRIVATE)
soFile = File(dir.toString() + "/" + getCpuAbi(appCtx), soName)
downloadFile = File(appCtx.cacheDir.toString() + "/so_download", soName)
Log.e(TAG, "soName+:$soName")

@ -31,8 +31,20 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
private val mBuffer = Buffer()
@Throws(IOException::class)
fun waitForDone(): Response {
fun waitForDone(urlRequest: UrlRequest): Response {
//获取okhttp call的完整请求的超时时间
val timeOutMs: Long = mCall.timeout().timeoutNanos() / 1000000
if (timeOutMs > 0) {
mResponseCondition.block(timeOutMs)
} else {
mResponseCondition.block()
}
//ConditionVariable 正常open或者超时open后,检查urlRequest是否完成
if (!urlRequest.isDone) {
urlRequest.cancel()
mException = IOException("Cronet timeout after wait " + timeOutMs + "ms")
}
if (mException != null) {
throw mException as IOException
}
@ -74,7 +86,7 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
// }
// Log.e("Cronet", sb.toString())
//打印协议,用于调试
Log.e("Cronet", info.negotiatedProtocol)
Log.i("Cronet", info.negotiatedProtocol)
if (eventListener != null) {
eventListener.responseHeadersEnd(mCall, this.mResponse)
eventListener.responseBodyStart(mCall)
@ -89,8 +101,8 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
byteBuffer: ByteBuffer
) {
byteBuffer.flip()
try {
//mReceiveChannel.write(byteBuffer)
mBuffer.write(byteBuffer)
} catch (e: IOException) {
Log.i(TAG, "IOException during ByteBuffer read. Details: ", e)
@ -104,8 +116,6 @@ class CronetUrlRequestCallback @JvmOverloads internal constructor(
eventListener?.responseBodyEnd(mCall, info.receivedByteCount)
val contentType: MediaType? = (this.mResponse.header("content-type")
?: "text/plain; charset=\"utf-8\"").toMediaTypeOrNull()
// val responseBody: ResponseBody =
// mBytesReceived.toByteArray().toResponseBody(contentType)
val responseBody: ResponseBody =
mBuffer.asResponseBody(contentType)
val newRequest = originalRequest.newBuilder().url(info.url).build()

@ -74,7 +74,7 @@ class DictViewModel(application: Application) : BaseViewModel(application) {
* @return
*/
fun isChinese(str: String): Boolean {
private fun isChinese(str: String): Boolean {
val p = Pattern.compile("[\u4e00-\u9fa5]")
val m = p.matcher(str)
return m.find()

@ -19,3 +19,5 @@ android.useAndroidX=true
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
android.enableResourceOptimizations=true

Loading…
Cancel
Save