pull/977/head
parent
b86339364d
commit
da0d070bfe
@ -1,40 +1,19 @@ |
|||||||
package io.legado.app.help.http.parser |
package io.legado.app.help.http.parser |
||||||
|
|
||||||
import io.legado.app.help.http.StrResponse |
import io.legado.app.help.http.StrResponse |
||||||
import io.legado.app.utils.EncodingDetect |
import io.legado.app.help.http.text |
||||||
import io.legado.app.utils.UTF8BOMFighter |
|
||||||
import okhttp3.Response |
import okhttp3.Response |
||||||
import rxhttp.wrapper.annotation.Parser |
import rxhttp.wrapper.annotation.Parser |
||||||
import rxhttp.wrapper.exception.HttpStatusCodeException |
import rxhttp.wrapper.exception.HttpStatusCodeException |
||||||
import java.nio.charset.Charset |
|
||||||
|
|
||||||
@Parser(name = "StrResponse") |
@Parser(name = "StrResponse") |
||||||
class StrResponseParser(private val encode: String? = null) : |
class StrResponseParser(private val encode: String? = null) : |
||||||
rxhttp.wrapper.parse.Parser<StrResponse> { |
rxhttp.wrapper.parse.Parser<StrResponse> { |
||||||
|
|
||||||
override fun onParse(response: Response): StrResponse { |
override fun onParse(response: Response): StrResponse { |
||||||
val body = getString(response) |
val body = response.body?.text(encode) |
||||||
|
?: throw HttpStatusCodeException(response, "内容为空") |
||||||
return StrResponse(response, body) |
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)) |
|
||||||
} |
|
||||||
|
|
||||||
} |
} |
Loading…
Reference in new issue