我的html字符串是这样的:
<meta http-equiv=\"Content-Type\" content=\"text/html\"; charset=\"UTF-8\" />
<p style="text-align: justify;"> paragraph </p>
<p style="text-align: justify;"> another one with <strong> strong attr </p>
<p style="text-align: justify;"> in general p have <strong> strong</strong> and <em> em parts</em></p>
我加载:
view.loadData(htmlString, "text/html", "UTF-8");
我有不同的html字符串,其中一些是好的,但其他人给我这个错误......问题出在哪里?
答案 0 :(得分:3)
解决了,给这个答案很多“帮助”,因为它真的是一个令人讨厌的webview错误,我想我的回答会对你们有所帮助!
如果您的html页面确实包含“%”,“\”或“#”字符之一,则loadData()方法将失败! 所以你必须手动替换这些chr,这是我的班级:
public class BuglessWebView extends WebView{
public BuglessWebView(Context context) {
super(context);
}
public BuglessWebView(Context context,AttributeSet attributes){
super(context,attributes);
}
public BuglessWebView(Context context,AttributeSet attributes,int defStyles){
super(context,attributes,defStyles);
}
@Override
public void loadData(String data, String mimeType, String encoding) {
super.loadData(solveBug(data), mimeType, encoding);
}
private String solveBug(String data){
StringBuilder sb = new StringBuilder(data.length()+100);
char[] dataChars = data.toCharArray();
for(int i=0;i<dataChars.length;i++){
char ch = data.charAt(i);
switch(ch){
case '%':
sb.append("%25");
break;
case '\'':
sb.append("%27");
break;
case '#':
sb.append("%23");
break;
default:
sb.append(ch);
break;
}
}
return sb.toString();
}
}
这是谷歌代码上的讨论链接:http://code.google.com/p/android/issues/detail?id=1733
答案 1 :(得分:3)
请改用loadDataWithBaseURL。
webView.loadDataWithBaseURL(null, html,"text/html", "UTF-8", null);
以下是有此解决方法的讨论: http://code.google.com/p/android/issues/detail?id=1733
评论:#14和#18
在这里工作。
答案 2 :(得分:1)
我刚刚遇到这个问题,并且有很多相关的错误报告与它有关。我有一个%是我的内联CSS导致页面没有被渲染。当我在WebView.loadData(...)
encoding参数指定数据是base64还是URL 编码。如果数据是base64编码的,则为编码值 参数必须是'base64'。对于参数的所有其他值, 包括null,假设数据使用ASCII编码 八位字节在安全URL字符范围内并使用标准 %xx对该范围之外的八位字节的URL的十六进制编码。例如, '#','%','\','?'应该被%23,%25,%27,%3f取代 分别
但是按照指示使用base64
没有任何区别。在我的4.3设备上一切都很好,但在2.3没有任何东西会呈现。查看所有错误报告,每个人都提出了不同的建议,但唯一对我有用的是使用
webView.loadDataWithBaseURL(null, data.content, "text/html", "UTF-8", null);
小心不要使用text/html;
代替text/html
,因为它会无声地失败!