[英]Android. WebView and loadData
可以使用以下方法设置 web 视图的内容 loadData(String data, String mimeType, String encoding)
如何处理html数据编码未知的问题?!
有编码列表吗?!
我从我的大学知道,在我的情况下,html 来自 DB 并用 latin-1 编码。 我尝试将编码参数设置为 latin-1,设置为 ISO-8859-1 / iso-8859-1,但仍然无法显示 ä、ö、ü 等特殊符号。
我将非常感谢您的任何建议。
myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);
这完美的作品,尤其是在Android 4.0,这显然忽略了内部HTML字符编码。
在 2.3 和 4.0.3 上测试。
事实上,我不知道最后一个参数除了“base64”之外还有什么其他值。 一些 Google 示例将 null 放在那里。
WebView.loadData() 根本无法正常工作。 我必须做的是:
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
myWebView.loadData(header+myHtmlString, "text/html", "UTF-8");
我认为在您的情况下,您应该在标题和 WebView.loadData() 中用 latin1 或 ISO-8859-1 替换 UTF-8。
而且,为了给出完整的答案,这里是编码的官方列表: http : //www.iana.org/assignments/character-sets
我更新了我的答案,使其更具包容性:
要将WebView.loadData()与非 latin1 编码一起使用,您必须对 html 内容进行编码。 上一个示例在 Android 4+ 中无法正常工作,因此我将其修改为如下所示:
WebSettings settings = myWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
String base64 = Base64.encodeToString(htmlString.getBytes(), Base64.DEFAULT);
myWebView.loadData(base64, "text/html; charset=utf-8", "base64");
} else {
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
myWebView.loadData(header + htmlString, "text/html; charset=UTF-8", null);
}
但是后来我切换到WebView.loadDataWithBaseURL()并且代码变得非常干净并且不依赖于 Android 版本:
WebSettings settings = myWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
myWebView.loadDataWithBaseURL(null, htmlString, "text/html", "utf-8", null);
出于某种原因,这些函数具有完全不同的实现。
据我了解, loadData()
只是生成一个data:
URL,其中包含提供它的数据。
阅读loadData()
的javadocs :
如果编码参数的值为'base64',则数据必须编码为base64。 否则,数据必须对安全 URL 字符范围内的八位字节使用 ASCII 编码,并为该范围外的八位字节使用 URL 的标准 %xx 十六进制编码。 例如,“#”、“%”、“\\”、“?” 应分别替换为 %23、%25、%27、%3f。
此方法形成的“数据”方案 URL 使用默认的 US-ASCII 字符集。 如果您需要设置不同的字符集,您应该形成一个“数据”方案 URL,它在 URL 的媒体类型部分明确指定一个字符集参数,并改为调用 loadUrl(String)。 请注意,从数据 URL 的媒体类型部分获取的字符集始终会覆盖 HTML 或 XML 文档本身中指定的字符集。
因此,您应该使用 US-ASCII 并自己转义任何特殊字符,或者仅使用 Base64 对所有内容进行编码。 假设您使用 UTF-8(我没有用 latin1 测试过),以下应该可以工作:
String data = ...; // the html data
String base64 = android.util.Base64.encodeToString(data.getBytes("UTF-8"), android.util.Base64.DEFAULT);
webView.loadData(base64, "text/html; charset=utf-8", "base64");
我有这个问题,但是:
String content = "<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /></head><body>";
content += mydata + "</body></html>";
WebView1.loadData(content, "text/html", "UTF-8");
不适用于所有设备。 我合并了一些方法:
String content =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+
"<html><head>"+
"<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"+
"</head><body>";
content += myContent + "</body></html>";
WebView WebView1 = (WebView) findViewById(R.id.webView1);
WebView1.loadData(content, "text/html; charset=utf-8", "UTF-8");
它有效。
在 Web 视图中加载 htmlContent 的最安全方法是:
“Base64 编码”是官方建议,已在 Chrominium的最新 01/2019 错误(存在于 WebView M72 (72.0.3626.76))中再次写入(已存在于 Javadoc 中):
https://bugs.chromium.org/p/chromium/issues/detail?id=929083
Chromium 团队的官方声明:
“建议修复:
我们的团队建议您使用 Base64 对数据进行编码。 我们提供了如何执行此操作的示例:
这个修复是向后兼容的(它适用于早期的 WebView 版本),并且还应该是面向未来的(你不会遇到关于内容编码的未来兼容性问题)。”
代码示例:
webView.loadData(
Base64.encodeToString(
htmlContent.getBytes(StandardCharsets.UTF_8),
Base64.DEFAULT), // encode in Base64 encoded
"text/html; charset=utf-8", // utf-8 html content (personal recommendation)
"base64"); // always use Base64 encoded data: NEVER PUT "utf-8" here (using base64 or not): This is wrong!
使用这个: String customHtml =text ;
wb.loadDataWithBaseURL(null,customHtml,"text/html", "UTF-8", null);
String strWebData="html...." //**Your html string**
WebView webDetail=(WebView) findViewById(R.id.webView1);
WebSettings websetting = webDetail.getSettings();
websetting.setDefaultTextEncodingName("utf-8");
webDetail.loadData(strWebData, "text/html; charset=utf-8", null);
上面的答案在我的情况下不起作用。 您需要在元标记中指定 utf-8
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<!-- you content goes here -->
</body>
</html>
webview.loadDataWithBaseURL(null, text, "text/html", "UTF-8", null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.