diff --git a/app/src/main/java/io/legado/app/help/http/OkHttpExtensions.kt b/app/src/main/java/io/legado/app/help/http/OkHttpExtensions.kt index e17fa35c4..d32d9f3d4 100644 --- a/app/src/main/java/io/legado/app/help/http/OkHttpExtensions.kt +++ b/app/src/main/java/io/legado/app/help/http/OkHttpExtensions.kt @@ -1,10 +1,14 @@ package io.legado.app.help.http +import io.legado.app.utils.EncodingDetect +import io.legado.app.utils.UTF8BOMFighter import kotlinx.coroutines.suspendCancellableCoroutine import okhttp3.Call import okhttp3.Callback import okhttp3.Response +import okhttp3.ResponseBody import java.io.IOException +import java.nio.charset.Charset import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException @@ -24,4 +28,22 @@ suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> } }) +} + +fun ResponseBody.text(encode: String? = null): String { + val responseBytes = UTF8BOMFighter.removeUTF8BOM(bytes()) + var charsetName: String? = encode + + charsetName?.let { + return String(responseBytes, Charset.forName(charsetName)) + } + + //根据http头判断 + contentType()?.charset()?.let { + return String(responseBytes, it) + } + + //根据内容判断 + charsetName = EncodingDetect.getHtmlEncode(responseBytes) + return String(responseBytes, Charset.forName(charsetName)) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt b/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt index 9868b9dc8..a5c8cdbe5 100644 --- a/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt +++ b/app/src/main/java/io/legado/app/help/http/parser/StrResponseParser.kt @@ -1,40 +1,19 @@ package io.legado.app.help.http.parser import io.legado.app.help.http.StrResponse -import io.legado.app.utils.EncodingDetect -import io.legado.app.utils.UTF8BOMFighter +import io.legado.app.help.http.text import okhttp3.Response import rxhttp.wrapper.annotation.Parser import rxhttp.wrapper.exception.HttpStatusCodeException -import java.nio.charset.Charset @Parser(name = "StrResponse") class StrResponseParser(private val encode: String? = null) : rxhttp.wrapper.parse.Parser { override fun onParse(response: Response): StrResponse { - val body = getString(response) + val body = response.body?.text(encode) + ?: throw HttpStatusCodeException(response, "内容为空") return StrResponse(response, body) } - private fun getString(response: Response): String { - - val responseBody = response.body ?: throw HttpStatusCodeException(response, "内容为空") - val responseBytes = UTF8BOMFighter.removeUTF8BOM(responseBody.bytes()) - var charsetName: String? = encode - - charsetName?.let { - return String(responseBytes, Charset.forName(charsetName)) - } - - //根据http头判断 - responseBody.contentType()?.charset()?.let { - return String(responseBytes, it) - } - - //根据内容判断 - charsetName = EncodingDetect.getHtmlEncode(responseBytes) - return String(responseBytes, Charset.forName(charsetName)) - } - } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt b/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt index 031ad9d33..1d4c4ec93 100644 --- a/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt +++ b/app/src/main/java/io/legado/app/help/http/parser/TextParser.kt @@ -1,11 +1,9 @@ package io.legado.app.help.http.parser -import io.legado.app.utils.EncodingDetect -import io.legado.app.utils.UTF8BOMFighter +import io.legado.app.help.http.text import okhttp3.Response import rxhttp.wrapper.annotation.Parser import rxhttp.wrapper.exception.HttpStatusCodeException -import java.nio.charset.Charset @Parser(name = "Text") class TextParser(private val encode: String? = null) : rxhttp.wrapper.parse.Parser { @@ -13,21 +11,7 @@ class TextParser(private val encode: String? = null) : rxhttp.wrapper.parse.Pars override fun onParse(response: Response): String { val responseBody = response.body ?: throw HttpStatusCodeException(response, "内容为空") - val responseBytes = UTF8BOMFighter.removeUTF8BOM(responseBody.bytes()) - var charsetName: String? = encode - - charsetName?.let { - return String(responseBytes, Charset.forName(charsetName)) - } - - //根据http头判断 - responseBody.contentType()?.charset()?.let { - return String(responseBytes, it) - } - - //根据内容判断 - charsetName = EncodingDetect.getHtmlEncode(responseBytes) - return String(responseBytes, Charset.forName(charsetName)) + return responseBody.text(encode) } } \ No newline at end of file