Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding issue with WebView's loadData

Tags:

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> 
like image 550
jul Avatar asked Oct 02 '11 10:10

jul


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

How do I display HTML content in WebView?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.


2 Answers

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.

like image 78
patryk Avatar answered Oct 05 '22 23:10

patryk


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?

like image 25
jul Avatar answered Oct 06 '22 00:10

jul