Adam
Adam

Reputation: 427

Character set in WebView (Android)

I am trying to display some data from a Java String in a WebView, using the loadData() method:

wv.loadData(myString, "text/html, "utf-8");

The issue I am having is that the WebView mangles the output of non-ASCII (I assume?) characters. If I use a TextView instead of a WebView, this problem does not occur, and the text displays correctly (although there is some HTML markup involved, so a TextView is not ultimately desirable).

If it helps, when I run the following code:

for(int i = 0; i < myString.length() && i < 400; i++)
      Log.i("Text", myString.charAt(i) + ": " + (int) myString.charAt(i));

an offending character appears as such in the log:

05-27 13:15:45.110: INFO/Text(606): â: 8217

This is a character set issue, I think, but I'm not quite sure how to resolve it.

Here's a snippet of the HTML (I'm not sure if my employer would allow a full posting of the content):

Tunisia’s prime minister

It is the ’ character that is causing issue. NB: What I'm displaying is not a fully-formed HTML page.. just text with HTML markup, if that matters..

Upvotes: 1

Views: 2939

Answers (2)

k3v
k3v

Reputation: 1189

I had a similar problem, where the string I was passing into the loadData() method was not being presented properly in the WebView (had odd characters instead of correct DBCS ones). In my case, the string was properly encoded in UTF-8 and the problem was in how loadData() was using the string.

I found that is important to specify the character set within the mime type argument, and not the encoding argument. Specifically the call had to be

wv.loadData(myString, "text/html; charset=utf-8", encoding);

In my case, I passed null as the encoding variable and it worked (Android 4.2.1). Reading the API documentation on loadData(); it seems to indicate that it will accept "base64" and all other values will cause the data argument to be treated as URL-encoded ASCII.

Therefore, you CANNOT set the encoding of the string with the encoding parameter, you must specify the encoding in the mime type.

Upvotes: 6

Sven Viking
Sven Viking

Reputation: 2720

Try:

wv.loadData(URLEncoder.encode(myString).replace("+", "%20"), "text/html, "utf-8");

Or, if that doesn't work:

wv.loadData(myString, "text/html, "ISO-8859-1");

Or a combination of the two. One of those generally works for me.

Upvotes: 1

Related Questions