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. Update to Platform Versions.

Would it be possible to get an update to




2. Please Help!!!!

I need farsi font!
And How Android Persian support?
Plese help


3. Query 1:

4. Sending custom headers in WebView

5. GDC 2010 Presentations

6. Customize SDK to Measure Tx Power

7. help regarding deploying my app