如何将K12中的win1251编码转换为UTF8?
我正在进行HTTP请求页面。 这个页面有西里尔文字符。 如何将CP1251中的答案转换为UTF8?
这是我的代码。
package bash import com.github.kittinunf.fuel.httpGet import com.github.kittinunf.result.Result fun main(args: Array) { val bashImHost = "http://bash.im/" bashImHost.httpGet().responseString { request, response, result -> when (result) { is Result.Failure -> { println("Some kind of error!") } is Result.Success -> { val htmlBody = result.value val parsePattern = "(.+)" val parseRegex = Regex(parsePattern) val results = parseRegex.findAll(htmlBody) results.iterator().forEach { resultItem -> println(resultItem.groups[1]?.value) } } } } }
我正在使用燃料HTTP库。
使用接受Charset
的responseString
重载来使用Charset.forName("Windows-1251")
来解码响应:
bashImHost.httpGet().responseString(Charset.forName("Windows-1251")) { request, response, result -> /* ... */ }
好像你无法在使用错误的编码UTF-8转换为String
之后,改变对Windows-1251的响应编码,请参阅本问答 。