WebView encoding? why?

by Angelo » Sat, 29 May 2010 06:55:13 GMT

Sponsored Links
 Hello all,

I have started to work with the WebView component of Android Platform,
but the API to load data to the component is confusing me, because it
includes a parameter with a encoding.

In both methods:
public void  loadData  (String data, String mimeType, String
public void  loadDataWithBaseURL  (String baseUrl, String data, String
mimeType, String encoding, String historyUrl);

you have to specify an encoding. In both cases, the API documentation
says that is is the encodign of the data (data parameter).

Well, as  know, if you provide data as a String object, the String is
internally stored in UTF-16 (unicode). To get a String object from a
external source, you need to specify the external source encoding to
convert it to UTF-16 and get the String internal representation
(String(byte[] data, String encoding)). So, at this moment, when you
have an String instance, everything from the original source has been
converted to UTF-16.
So, why do you need to specify an encoding if you are providing the
data as a String object?

I don't know the internal implementation of WebKit, but let's suppose
that, WebKit has a method, to render a source, providing as content an
InputStream and a default encoding. Well, as I know, when you process
a HTML page, you use the default encoding to read the HTML characters,
and if you find a <meta> tag specifing a different encoding, from that
moment, you have to use the new encoding to decode the characters of
the HTML page.

But what happens if you supply a HTML page that contains this <meta>
<meta HTTP-EQUIV="content-type" CONTENT="text/html;

using one of the methods specified at the top of this post,
how are you going to read ISO-8859-1 encoding from a String that it is
in UTF-16 encoding?
if the WebKit filtering this <meta> tags, and doesn't taking in
account the encoding specified in this tag?

Well, all of this is very confusing for me, because I don't understand
why the API includes this encoding parameter. I also don't know if the
<meta> encoding tags are filtered when the source is provided as a

Can anyone solve my doubts?

Thanks in advance


WebView encoding? why?

by Frank Weiss » Sat, 29 May 2010 11:11:42 GMT

 I think your puzzlement is well justified. It would seem to make more sense
that the first parameter should be byte[] instead of String.

Without looking at the Android source, I have some hypothesis:

1. The loadUrl() method indicates mimeType may be "image/jpeg" and encoding
may be "base64". In this case it would be reasonable that although the first
parameter is String, the correct loading of the data would require knowing
the encoding (although I don't know what other encoding may be reasonable).
2. In the absence of the ContentType HTTP header, you need to specify the
second and third parameters, perhaps because the WebView methods delegate to
another class/method that does require them.
3. Perhaps the encoding parameter is entirely ignored in the case that the
mimeType is text/*. That's where looking at the source would be helpful.


Sponsored Links

Other Threads

1. Using OS 1.6 and Up , are we penalized on the market ?

We shall explain and handle case by case our potential users's concerns and educated them about our application. Like previously said, state into the app description the reason why the permission is needed is a good start :)
If you don't have room in the app description, put it on the website that your Market listing links to. That's what I do - list the app's permissions, and why each is needed, in an FAQ on my linked site. And I've yet to receive a single e-mail from a prospective user asking why the permissions are needed.
For the record, I've done exactly this on one of my apps, PLUS a big button on the main screen that links to the very same FAQ, and I still get a fair amount of users who fail to read it... 8-/ I guess this is a sign of hitting the mass market as opposed to hi-tech geeks, so it's not all bad. (In my case, the application is very much Sweden specific, so everything is in Swedish except, obviously, the Market as that is still not an option.) Best / Jonas --

2. Setting wifi AP while wireless is switched off

I think I found the answer myself.

It seems Android directly push the settings to the WPA-Supplicant,
which is not available with wifi switched off. So (at the moment) it
seems to be impossible to control this settings without activating

Henning Rogge


3. Extract one example from a project with many

4. ContextMenu on TableLayout: how to get selected item position?

5. ViewFlipper switching views on screen orientation change

6. Salam kenal... Again..

7. pressing home button to launch the web browser