网页视图:网页不可用,但是我从一个HTML字符串加载它 [英] WebView: webpage not available but I load it from an html string

查看:94
本文介绍了网页视图:网页不可用,但是我从一个HTML字符串加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML字符串是这样的:

My html string is like this:

<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字符串,其中有些是好的,但别人给我这个错误......问题出在哪里?

I have different html string, some of them are ok, but others give me this error...where is the problem?

推荐答案

解决了,给我一个很大的有用这个答案,因为它真的是一个讨厌的WebView错误,我想我的回答将有很大的帮助你!

Solved, gimme a lot of "helpful" for this answer because it's really a nasty webview bug and I think my answer will help a lot of you!

如果你的HTML页面中包含,事实上,%一,\\或#字符,loadData()方法将失败!
所以,你必须手动替换这些字符,这里是我的类:

If your html page contains, indeed, one of "%","\" or "#" characters, loadData() method will fail!! So you have to manually replace these chr and here's my class:

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();
}
}

在谷歌code在这里的讨论链接: HTTP :?//$c$c.google.com/p/android/issues/detail ID = 1733

这篇关于网页视图:网页不可用,但是我从一个HTML字符串加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆