WebView: the webpage is not available, but I am loading it from the html line.

My html line looks 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:

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

I have another html line, some of them are fine, but others give me this error ... where is the problem?

+2
source share
3 answers

Resolved, give me a lot of “useful” for this answer, because it is really a nasty web search error, and I think my answer will help many of you!

If your html page really contains one of the characters "%", "\" or "#", the loadData () method will fail! So you need to manually replace this chr and here is 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 is the discussion link in google code: http://code.google.com/p/android/issues/detail?id=1733

+3
source

Use loadDataWithBaseURL instead.

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

This workaround is discussed here: http://code.google.com/p/android/issues/detail?id=1733

Comments: # 14 and # 18

Worked here.

+2
source

I just ran into this problem, and there are many related error messages there. I had% s my inline CSS, as a result of which the page was not rendered. I thought everything was fine when I saw WebView.loadData(...) in the docs

The encoding parameter indicates 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 parameter values, including null, it is assumed that the data uses ASCII encoding for octets within the range of safe URLs and use the standard% xx hex URL encoding for octets outside this range. For instance, '#', '%', '\', '?' should be replaced by% 23,% 25,% 27,% 3f, respectively.

but alas, using base64 , as indicated, doesn't matter. On my 4.3 device everything was fine, but on 2.3 it didn’t show anything. Looking at all the bug reports, everyone was offering different things, but the only thing that worked for me was to use

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

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

0
source

Source: https://habr.com/ru/post/1438560/


All Articles