android - 显示 windows-1250 字符集 html 的 android webview 出现问题

标签 android character-encoding webview

我想将 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 源作为字符串。你可以看到下面的结果,字符编码显然是乱七八糟的。

Webview wrong encoding Windows-1250

从 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/

相关文章:

单一 View 中的Android图像和文本

java - Android Java开发: App Stopped Working

PHP:西里尔(俄语)字符作为问号回显。为什么?

javascript - WebView 代码在 Android 4.4.2 (API 19) 模拟器上生成 Uncaught TypeError 和 Uncaught ReferenceError 错误

java - 不同的加密 Android 与纯 Java - RSA/ECB/OAEPWithMD5AndMGF1Padding

http - jdk.incubator.httpclient URI 查询字符串在包含编码的保留字符时被截断

utf-8 - 如何确定文件编码?

javascript - 捕获 <webview> 中由 HTML 标签引起的 "Failed to load resources"错误

android - 如何在向下滚动网页 View 时隐藏操作栏?

javascript - 如何将文件从 PhoneGap/webapp ftp 到服务器?