Phate
Phate

Reputation: 6602

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

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?

Upvotes: 2

Views: 2578

Answers (3)

Dori
Dori

Reputation: 18403

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!

Upvotes: 1

Vinicius Maciel
Vinicius Maciel

Reputation: 31

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.

Upvotes: 3

Phate
Phate

Reputation: 6602

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

Upvotes: 3

Related Questions