21

I'm loading some data, containing latin-1 characters, in a WebView using

String uri = Uri.encode(html);
webview.loadData(uri, "text/html", "ISO-8859-1");

When displayed, the latin1 characters are replaced by weird characters.

If I load the html directly in a TextView (just to test), latin characters are properly displayed.

Anybody can help?

Thanks

html:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <!-- some html -->

</html>
jul
  • 36,404
  • 64
  • 191
  • 318

8 Answers8

52
myWebView.loadData(myHtmlString, "text/html; charset=UTF-8", null);

This works flawlessly, especially on Android 4.0, which apparently ignores character encoding inside HTML.

Tested on 2.3 and 4.0.3.

In fact, I have no idea about what other values besides "base64" does the last parameter take. Some Google examples put null in there.

You should always use UTF-8 encoding. Every other character encoding has become obsolete for many years already.

patryk
  • 2,747
  • 1
  • 18
  • 8
  • This is an interesting hack... but works for me on Android 4.3! Thanks! – Chiubaka Sep 16 '13 at 07:21
  • Thanks, a great hack indeed. Tested OK on Android 4.4.2. – rolgalan Sep 28 '15 at 14:45
  • String is in Unicode format in Java, how do i encode it with utf-8? – Shawn Jun 18 '16 at 22:14
  • @Shawn: Unicode is standard, UTF-8 is one of the encodings for Unicode. Android uses UTF-8 internally. You should be fine without any changes. – patryk Jun 22 '16 at 09:51
  • I'm sorry - can you show me how to encode a java String to UTF-8? it is UCS-2 internally. Note I'm saying String, not a byte stream; Any way, I guess it's irrelevant. It just tell the browser the encoding of this data scheme is not US-ASCII – Shawn Jun 24 '16 at 18:05
35

Only way to have it working, as commented here:

webview.loadDataWithBaseURL("fake://not/needed", html, "text/html", "utf-8", "");

No URI encoding, utf-8... loadData bug?

jul
  • 36,404
  • 64
  • 191
  • 318
  • I think this thread proposes a more elegant solution: http://stackoverflow.com/questions/7412763/string-encoding-problem-in-webview – Sparky Feb 09 '12 at 18:10
  • I'm not being able to show the inverted exclamation point (¡, ¡, ¡) using any of these methods. Does anyone know how to work this around? – Cassio Landim Jul 10 '13 at 12:54
14
String start = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /></head><body>";
String end = "</body></html>";

webcontent.loadData(start+ YOURCONTENT + end, "text/html; charset=UTF-8", null);

One of solution of problem.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Göksel Güren
  • 1,479
  • 13
  • 21
  • This is the only thing that works for me. Decodes \u chars (like \u016b etc.) – Rooster242 Jul 08 '14 at 03:55
  • Awesome! that worked as expected. BTW I didn't use null as the last parameter: webcontent.loadData(start+ YOURCONTENT + end, "text/html; charset=UTF-8", "UTF-8"); – user3193413 Apr 08 '18 at 08:04
8

I have display © 2011 and it was displaying ©.

With the below code i have achieved displaying correct value © 2011

webViewContent.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
Botz3000
  • 39,020
  • 8
  • 103
  • 127
Pritesh Shah
  • 857
  • 1
  • 7
  • 8
1
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); 
Taryn
  • 242,637
  • 56
  • 362
  • 405
Mahmoud Badri
  • 1,256
  • 14
  • 24
0

I too had the problem of getting a weird character like  here and there. Tried different options, but the one that worked is below.

String style_sheet_url = "http://something.com/assets/css/layout.css";
    String head = "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" + 
            "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + style_sheet_url + "\" /></head>";    
    String locdata = "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + head + "<body>"+ data + "</body></html>";
    wv_news_text.loadData(locdata, "text/html", "utf-8");

wv_news_text is the WebView.

DanKodi
  • 3,550
  • 27
  • 26
0

Info from Java docs about loadData method

Loads the given data into this WebView using a 'data' scheme URL.

Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL.

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.

The 'data' scheme URL formed by this method uses the default US-ASCII charset. If you need need to set a different charset, you should form a 'data' scheme URL which explicitly specifies a charset parameter in the mediatype portion of the URL and call loadUrl(String) instead. Note that the charset obtained from the mediatype portion of a data URL always overrides that specified in the HTML or XML document itself.

Following code worked for me.

String base64EncodedString = null;
                        try {
                            base64EncodedString = android.util.Base64.encodeToString((preString+mailContent.getBody()+postString).getBytes("UTF-8"), android.util.Base64.DEFAULT);
                        } catch (UnsupportedEncodingException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        if(base64EncodedString != null)
                        {
                            wvMailContent.loadData(base64EncodedString, "text/html; charset=utf-8", "base64");  
                        }
                        else
                        {
                            wvMailContent.loadData(preString+mailContent.getBody()+postString, "text/html; charset=utf-8", "utf-8");
Dev.Sinto
  • 6,802
  • 8
  • 36
  • 54
0

AFAIK that: Firstly, loadData() method is used to load raw html code.
Secondly, just put the html code directly to the loadData(), don't encode it

You might wanna try like this:

webview.loadData(uri, "text/html", "ISO-8859-1");

Cheers!

Pete Houston
  • 14,931
  • 6
  • 47
  • 60