Kleptine
Kleptine

Reputation: 5085

WebView not Loading Data?

I'm trying to load the parsed html data from an rss feed using a WebView, but the webview claims that the page:

"data:text/html;utf-8,[The html I'm trying to display]"

is not available.

alt text

I find it strange that it seems to be putting the html data into the url, when I just want it to display it.

Here's my code right now for the webview:

Bundle data = getIntent().getExtras();

WebView webview = new WebView(this);
setContentView(webview);

webview.loadData(data.getString("DEFAULTTEXT"), "text/html", "utf-8");

Where the HTML has been passed in a string in the Bundle with the identifier: DEFAULTTEXT. I've tested the class and the HTML is passed fine, it just isn't displayed correctly.

It works fine on some of the webpages I've tried, but not others. I'll try to post the code of one that works and one that doesn't.

Upvotes: 6

Views: 3300

Answers (4)

georgiecasey
georgiecasey

Reputation: 23391

As stated, when you have characters like '%', '\', '#' in your HTML, it needs to be escaped which loadData doesn't seem to do automatically.

loadDataWithBaseURL instead of loadData does escape and seems to fix this. Just use null for baseUrl and historyUrl. So the example code in the question is changed to:

Bundle data = getIntent().getExtras();

WebView webview = new WebView(this);
setContentView(webview);

webview.loadDataWithBaseURL(null,data.getString("DEFAULTTEXT"), "text/html", "utf-8",null);

Upvotes: 2

Reza Nezami
Reza Nezami

Reputation: 31

I believe this sporadic behavior of loadData* is because of what is the content of the page you are trying to load. If it is plain simple html it has no problem. But if it has components like css or other features requiring external info, it will bail out. My experience!

Upvotes: 1

Kleptine
Kleptine

Reputation: 5085

Huuu.... so I turned my computer on this morning and it worked perfectly. I still don't know what the problem was. :/

Edit: Never mind. It works on some, but not all of the pages I try to display.

Edit2: swapping it out for loadDataWithBaseURL worked like a charm.

Upvotes: 3

Faisal Abid
Faisal Abid

Reputation: 9140

Are you sure you have the correct permissions defined in the manifest.xml?

Upvotes: 0

Related Questions