87116

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?

Answer1:

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

Answer2:

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.

Answer3:

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!

Recommend

  • android 2.2 WebView and WebViewClient RTSP problem
  • Use shared WKWebViewCookies for a request
  • setImageData fails in iOS 8.3
  • Using Xcode Playground captureValue()
  • Getting access to a binary response byte-by-byte in classic asp/JScript
  • PHP Script in my Wordpress Files. Please Decode or Help Me Remove it [closed]
  • Towers of Hanoi with “counter” in python
  • how to limit tornado websocket message size
  • Phonegap Android App, open links within app?
  • C++ exception safety paranoia: how much is too much?
  • ASP.NET MVC - CSS Class in a ViewUserControl - The class or CssClass is not defined
  • How do I make openssl_encrypt pad the input to the required block size?
  • input type=“file” accept=“image/*” doesn't work in phone gap?
  • An unexpected error has been detected by HotSpot Virtual Machine
  • iPhone - UIWebView multiple locks
  • How to get to older Xcode beta version?
  • Safari PHP form submission -file upload hangs
  • Why is OpenID Connect considered mobile friendly compared to SAML
  • setContentView() is not enough to switch between layouts?
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • SAXReader not re-ecape characters
  • how does System.Web.HttpRequest::PathInfo work?
  • how to avoid repetitive constructor in children
  • Test if a set exists before trying to drop it
  • Chrome doesn't support silverlight anymore? How to solve this?
  • Checking free space on FTP server
  • Highlight and Bold text in JTextPane
  • how to do an event when i swipe from fragment to the other
  • RectangularRangeIndicator format like triangular using dojo
  • Akka Routing: Reply's send to router ends up as dead letters
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?