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

前端 未结 3 606
一个人的身影
一个人的身影 2021-01-15 02:06

My html string is like this:


相关标签:
3条回答
  • 2021-01-15 02:25

    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!

    0 讨论(0)
  • 2021-01-15 02:35

    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.

    0 讨论(0)
  • 2021-01-15 02:36

    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

    0 讨论(0)
提交回复
热议问题