mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user