我想将 Windows-1250 编码的 html 页面加载到 WebView 中。其实我不喜欢,但我不得不。可以找到此编码的示例 here .
我可以在任何 pc 浏览器中正常查看上述页面 - android webview 也可以正确显示该页面。
不过,我需要做的是获取上述页面的 base64 编码版本,并将其从 String 资源加载到 webview 中。所以,作为测试,我使用了 this online tool获取页面的 base64 编码版本,作为字符串添加到我的应用程序中,并尝试通过
加载它myWebView.loadData(htmlResource, "text/html; charset=Windows-1250", "base64");
,其中 htmlResource 包含 base64 编码的 html 源作为字符串。你可以看到下面的结果,字符编码显然是乱七八糟的。
从 base64 编码的字符串中显示此页面的正确方法是什么?
编辑:我也试过this方法,结果相同:
String decodedResource = new String(Base64.decode(htmlResource));
mWebView.loadDataWithBaseURL( null, decodedResource, "text/html",
"Windows-1250", null );
编辑 2:我还尝试了 snoblucha 的建议并进行了以下修改,但仍然没有成功:
try {
convertedResource = new String(Base64.decode(htmlResource), "windows-1250");
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncodingException", e.getMessage());
}
mWebView.loadData(convertedResource, "text/html", "windows-1250");
编码仍然是困惑的,尽管略有不同。
最佳答案
尝试使用这段代码:
String html = "Some string in windows-1250"; // Actually string in unicode
String encoded = Base64.encodeToString(html.getBytes("cp1250"), Base64.DEFAULT); // Convert to array of bytes in cp1250 and later to base64 string
webView.loadData(encoded, "text/html; charset=windows-1250", "base64"); // Load to WebView
另见 question .
关于android - 显示 windows-1250 字符集 html 的 android webview 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11307187/