webview problems when rendering HTML and using loadData

by Clay » Mon, 02 Mar 2009 09:51:57 GMT


Sponsored Links
 I know that many of you know this already but I am making a post
because I spent 5 hours trying to figure out why loadData was not
working. This does not pop up easily in searches of the mailing list.

TAGS: webview background white android loadData

WebView momentContent =
                        (WebView) view.findViewById
(R.id.subscription_list_moment_webcontent);
WebSettings wsettings =
                        momentContent.getSettings();
wsettings.setJavaScriptEnabled(true);

//works great
//momentContent.loadUrl(" http://www.momentarynow.com/app/public/ 
android/info_about.html");

//doesnt work exact same content WHY?
momentContent.loadData(
                                ResourceUtils.loadResToString(R.raw.content, 
mContext),
                                mimeType,
                                encoding);

====

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
.bg {
        font-size: x-small;
        background-color: #CC6600;
}
-->
</style>
</head>
<body class="bg">
foobar
</body>
</html>

unless you use the most simple html possible, like no style sheets,
backgrounds, etc, loadData seems to blow up. The only workaround I
have found is to use loadDataWithBaseURL instead


                final String mimeType = "text/html";
                final String encoding = "UTF-8";
                //render the html
                WebView momentContent =
                        (WebView) 
findViewById(R.id.subscription_list_moment_webcontent);
                WebSettings wsettings =
                        momentContent.getSettings();
                wsettings.setJavaScriptEnabled(true);
                momentContent.loadDataWithBaseURL(" http://fake.com ",
                                ResourceUtils.loadResToString(
                                                R.raw.content,
                                                getApplicationContext()),
                                mimeType,
                                encoding,
                                " http://fake.com ");


oh now it magically works. but my forehead is a bit bruised from
hammering it against the brick wall.

I merely post to help others avoid my fate.

Clay



--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. looking for good media player

I HATE that the media player in android starts the song over when you
have to take a call or when you leave the media player for an extended
period of time or restart the phone.  I was using beyondpod for my
media player but now they want 5 bucks, (not really worth it ive
already spent money on another rss reader i just want the media
player)  Does anyone know of a media player free or not that will
remember the place where you leave off?
--~--~---------~--~----~------------~-------~--~----~

2. v1.1 included with v1.5 isn't identical to old v1.1?!

My code builds fine under the old v1.1_r1 SDK.  I've been working with
it like that for months.  With 1.5 installed, I can no longer build my
project, even when I target v1.1.  I get a Dalvik conversion failure
type 2.  How can the 1.1 included with 1.5 fail when the old 1.1
works?  This is VERY frustrating.  I am at a total loss for how to fix
this.  I really expected the 1.1 included with 1.5 to work exactly
like the old 1.1.

In addition, I'm pretty unclear as to how to revert to the old 1.1 SDK
so I can get up and running again, even if it's with the older
system.  At least that worked.  Eclipse seems really locked into the
new SDK now.  I tried reverting through the software updates interface
but...I'm not sure...it's not really working, I'm unclear what the
problem is.

Is it easy to roll back to an earlier SDK or am I totally dead in the
water if I never figure out how to solve this Dalvik conversion
problem?

Thanks.
--~--~---------~--~----~------------~-------~--~----~

3. Running two applications in the same process

4. how to test skia application using OpenGL backend

5. Are G1 consumers paying for apps or app updates?

6. Starting Understand the Source: Prerequisite

7. Can SlidingDrawer stop sliding and just stay there with motionevents?