WebView: webpage not available but I load it from an html string

你说的曾经没有我的故事 提交于 2020-01-11 07:06:48

问题


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>

and I load with:

view.loadData(htmlString, "text/html", "UTF-8");

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


回答1:


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!

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

here's discussion link on google code: http://code.google.com/p/android/issues/detail?id=1733




回答2:


Use loadDataWithBaseURL instead.

 webView.loadDataWithBaseURL(null, html,"text/html", "UTF-8", null);

Here's the discussion that has this workaround: http://code.google.com/p/android/issues/detail?id=1733

Comments : #14 and #18

Worked here.




回答3:


I just encountered this issue and there quite a few related bug reports to do with it. I had an % is my inline CSS which resulted in the page not being rendered. I thought all was ok when I saw in the docs for WebView.loadData(...)

The encoding parameter specifies whether the data is base64 or URL encoded. If the data is base64 encoded, the value of the encoding parameter must be 'base64'. For all other values of the parameter, including null, it is assumed that the data uses ASCII encoding for octets inside the range of safe URL characters and use the standard %xx hex encoding of URLs for octets outside that range. For example, '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.

but alas using base64 as instructed makes no difference. On my 4.3 device everything was fine but on 2.3 nothing would render. Looking at all the bug reports everyone suggested different stuff but the only thing that worked for me was using

webView.loadDataWithBaseURL(null, data.content, "text/html", "UTF-8", null);

be careful not to use text/html; instead of text/html as it will silently fail!



来源:https://stackoverflow.com/questions/12332929/webview-webpage-not-available-but-i-load-it-from-an-html-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!