Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Webpage not available' with WebView.loadData() ONLY in emulator

I'm calling loadData on my WebView and passing it some HTML in the form of a String like so:

webView.loadData( htmlString, "text/html", "utf-8" );

It works fine on my Galaxy Tab 10.1, but the WebView displays:

Webpage not available

when running on the emulator with everything set up to match my Galaxy Tab. Setting android.permission.INTERNET in the manifest has no effect, though I shouldn't need that permission since I'm rendering in-memory HTML, and not accessing anything over the data connection.

What's going on?

like image 619
user1086395 Avatar asked Dec 07 '11 19:12

user1086395


2 Answers

Try with this code

webView.loadData( URLEncoder.encode(htmlString).replaceAll("\\+"," "), "text/html", "utf-8" );

insted of

webView.loadData( htmlString, "text/html", "utf-8" );

it should work, because sometimes character like '%', '\', '#' creates problem if its not properly Encoded

like image 172
Viraj Avatar answered Oct 22 '22 18:10

Viraj


In 2.x platforms loadData() fails in some cases (it requires the html to be escaped), use loadDataWithBaseURL() instead and pass null for baseUrl and historyUrl:

webView.loadDataWithBaseURL(null, htmlString, "text/html", "utf-8", null);
like image 30
fardjad Avatar answered Oct 22 '22 18:10

fardjad