This commit is contained in:
kunfei
2023-06-11 19:52:42 +08:00
parent 48550be1bb
commit f409ef6ec4
3 changed files with 18 additions and 23 deletions

View File

@@ -3,14 +3,19 @@ package io.legado.app.help.http
import io.legado.app.utils.EncodingDetect
import io.legado.app.utils.GSON
import io.legado.app.utils.Utf8BomUtils
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import okhttp3.*
import okhttp3.Call
import okhttp3.Callback
import okhttp3.FormBody
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.ResponseBody
import java.io.File
import java.io.IOException
import java.nio.charset.Charset
@@ -21,18 +26,16 @@ suspend fun OkHttpClient.newCallResponse(
retry: Int = 0,
builder: Request.Builder.() -> Unit
): Response {
return withContext(IO) {
val requestBuilder = Request.Builder()
requestBuilder.apply(builder)
var response: Response? = null
for (i in 0..retry) {
response = newCall(requestBuilder.build()).await()
if (response.isSuccessful) {
return@withContext response
}
val requestBuilder = Request.Builder()
requestBuilder.apply(builder)
var response: Response? = null
for (i in 0..retry) {
response = newCall(requestBuilder.build()).await()
if (response.isSuccessful) {
return response
}
return@withContext response!!
}
return response!!
}
suspend fun OkHttpClient.newCallResponseBody(

View File

@@ -15,7 +15,6 @@ import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.isTrue
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.ensureActive
@@ -88,11 +87,8 @@ object BookChapterList {
"◇并发解析目录,总页数:${chapterData.second.size}"
)
withContext(IO) {
//页数太多并行访问有问题,这里判断下页数,超过5页就不并行访问
val asyncStart =
if (chapterData.second.size > 5) CoroutineStart.LAZY else CoroutineStart.DEFAULT
val asyncArray = Array(chapterData.second.size) {
async(IO, start = asyncStart) {
async(IO) {
val urlStr = chapterData.second[it]
val res = AnalyzeUrl(
mUrl = urlStr,

View File

@@ -14,7 +14,6 @@ import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.ensureActive
@@ -105,11 +104,8 @@ object BookContent {
} else if (contentData.second.size > 1) {
Debug.log(bookSource.bookSourceUrl, "◇并发解析正文,总页数:${contentData.second.size}")
withContext(IO) {
//页数太多并行访问有问题,这里判断下页数,超过5页就不并行访问
val asyncStart =
if (contentData.second.size > 5) CoroutineStart.LAZY else CoroutineStart.DEFAULT
val asyncArray = Array(contentData.second.size) {
async(IO, start = asyncStart) {
async(IO) {
val urlStr = contentData.second[it]
val res = AnalyzeUrl(
mUrl = urlStr,