Tim*_*ima 102 encoding android webview
可以使用以下方法进行内容设置web-view loadData(String data,String mimeType,String encoding)
如何处理未知的HTML数据编码问题?!
有编码列表吗?!
我从我的大学知道,在我的情况下,html来自数据库,并使用latin-1编码.我尝试将编码参数设置为latin-1,设置为ISO-8859-1/iso-8859-1,但仍然存在显示ä,ö,ü等特殊标志的问题.
我会非常感谢任何建议.
pat*_*ryk 206
myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);
Run Code Online (Sandbox Code Playgroud)
这完美的作品,尤其是在Android 4.0,这显然忽略了字符编码中的HTML.
测试2.3和4.0.3.
事实上,我不知道除了"base64"之外的其他值是什么.一些Google示例在那里放置了null.
And*_*kov 133
WebView.loadData()根本无法正常工作.我必须做的是:
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
myWebView.loadData(header+myHtmlString, "text/html", "UTF-8");
Run Code Online (Sandbox Code Playgroud)
我认为在你的情况下你应该在头文件和WebView.loadData()中用latin1或ISO-8859-1替换UTF-8.
并且,为了给出完整的答案,这里是正式的编码列表:http://www.iana.org/assignments/character-sets
我更新我的答案更具包容性:
要使用非latin1编码的WebView.loadData(),您必须编码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);
}
Run Code Online (Sandbox Code Playgroud)
但后来我切换到WebView.loadDataWithBaseURL(),代码变得非常干净,不依赖于Android版本:
WebSettings settings = myWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
myWebView.loadDataWithBaseURL(null, htmlString, "text/html", "utf-8", null);
Run Code Online (Sandbox Code Playgroud)
由于某些原因,这些功能具有完全不同的实现
Ral*_*alf 35
据我了解,loadData()
只需生成一个data:
包含数据的URL 即可.
阅读的javadoc为loadData()
:
如果encoding参数的值为'base64',则必须将数据编码为base64.否则,数据必须对安全URL字符范围内的八位字节使用ASCII编码,并对该范围外的八位字节使用URL的标准%xx十六进制编码.例如, '#', '%', '\', '?' 应分别替换为%23,%25,%27,%3f.
此方法形成的"数据"方案URL使用默认的US-ASCII字符集.如果您需要设置不同的字符集,则应该形成一个"数据"方案URL,该URL明确指定URL的mediatype部分中的charset参数,并调用loadUrl(String).请注意,从数据URL的mediatype部分获取的charset始终覆盖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");
Run Code Online (Sandbox Code Playgroud)
Alo*_*try 20
我有这个问题,但是:
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");
Run Code Online (Sandbox Code Playgroud)
不适用于所有设备.我合并了一些方法:
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");
Run Code Online (Sandbox Code Playgroud)
有用.
Pas*_*cal 10
在 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!
Run Code Online (Sandbox Code Playgroud)
小智 5
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);
Run Code Online (Sandbox Code Playgroud)
小智 5
使用此:String customHtml = text;
wb.loadDataWithBaseURL(null,customHtml,"text/html", "UTF-8", null);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
128421 次 |
最近记录: |