From a950f9ec3dd2310e326a5ecf71573f2883faf13e Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 9 Sep 2021 22:57:56 +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/OkHttpUtils.kt | 42 +++++++++++-------- .../java/io/legado/app/model/CacheBook.kt | 4 +- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt index b5dc53a84..b46b30b5a 100644 --- a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt +++ b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt @@ -4,7 +4,9 @@ import io.legado.app.constant.AppConst import io.legado.app.help.AppConfig import io.legado.app.utils.EncodingDetect import io.legado.app.utils.UTF8BOMFighter +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.withContext import okhttp3.* import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.MediaType.Companion.toMediaType @@ -18,34 +20,38 @@ suspend fun OkHttpClient.newCall( retry: Int = 0, builder: Request.Builder.() -> Unit ): ResponseBody { - val requestBuilder = Request.Builder() - requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) - requestBuilder.apply(builder) - var response: Response? = null - for (i in 0..retry) { - response = this.newCall(requestBuilder.build()).await() - if (response.isSuccessful) { - return response.body!! + return withContext(IO) { + val requestBuilder = Request.Builder() + requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) + requestBuilder.apply(builder) + var response: Response? = null + for (i in 0..retry) { + response = this@newCall.newCall(requestBuilder.build()).await() + if (response.isSuccessful) { + return@withContext response.body!! + } } + return@withContext response!!.body ?: throw IOException(response.message) } - return response!!.body ?: throw IOException(response.message) } suspend fun OkHttpClient.newCallStrResponse( retry: Int = 0, builder: Request.Builder.() -> Unit ): StrResponse { - val requestBuilder = Request.Builder() - requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) - requestBuilder.apply(builder) - var response: Response? = null - for (i in 0..retry) { - response = this.newCall(requestBuilder.build()).await() - if (response.isSuccessful) { - return StrResponse(response, response.body!!.text()) + return withContext(IO) { + val requestBuilder = Request.Builder() + requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) + requestBuilder.apply(builder) + var response: Response? = null + for (i in 0..retry) { + response = this@newCallStrResponse.newCall(requestBuilder.build()).await() + if (response.isSuccessful) { + return@withContext StrResponse(response, response.body!!.text()) + } } + return@withContext StrResponse(response!!, response.body?.text() ?: response.message) } - return StrResponse(response!!, response.body?.text() ?: response.message) } suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> diff --git a/app/src/main/java/io/legado/app/model/CacheBook.kt b/app/src/main/java/io/legado/app/model/CacheBook.kt index ab88bbfab..766069e33 100644 --- a/app/src/main/java/io/legado/app/model/CacheBook.kt +++ b/app/src/main/java/io/legado/app/model/CacheBook.kt @@ -70,10 +70,10 @@ class CacheBook(var bookSource: BookSource, var book: Book) { } } - fun start(context: Context, bookUrl: String, start: Int, end: Int) { + fun start(context: Context, book: Book, start: Int, end: Int) { context.startService { action = IntentAction.start - putExtra("bookUrl", bookUrl) + putExtra("bookUrl", book.bookUrl) putExtra("start", start) putExtra("end", end) }