pull/540/head
gedoor 4 years ago
parent ba6fc2e5d1
commit ccedfdbfd3
  1. 4
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  2. 33
      app/src/main/java/io/legado/app/help/http/TextParser.kt

@ -7,8 +7,8 @@ import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.*
import retrofit2.Retrofit
import rxhttp.toStr
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.concurrent.TimeUnit
@ -44,7 +44,7 @@ object HttpHelper {
suspend fun simpleGetAsync(url: String): String {
val str = RxHttp.get(url)
.addHeader(AppConst.UA_NAME, AppConfig.userAgent)
.toStr()
.toText()
return str.await()
}

@ -0,0 +1,33 @@
package io.legado.app.help.http
import io.legado.app.utils.EncodingDetect
import io.legado.app.utils.UTF8BOMFighter
import okhttp3.Response
import rxhttp.wrapper.annotation.Parser
import rxhttp.wrapper.exception.HttpStatusCodeException
import java.nio.charset.Charset
@Parser(name = "Text")
class TextParser(val encode: String? = null) : rxhttp.wrapper.parse.Parser<String> {
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))
}
}
Loading…
Cancel
Save