Reputation: 105
I'm trying to load a static HTML Page into a webview.
When I change its contents to a simple html page it works. So I believe something wrong with this html file, however the file is viewed correctly on Mozilla and Chrome. so my questions are
The html file http://snipt.org/vagL9
Screenshot
Upvotes: 2
Views: 4550
Reputation: 9850
I was facing the same issue.
Solved this by applying the code below
WebView webView = (WebView) findViewById(R.id.webView1);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(location);
HttpResponse response = httpClient.execute(httpGet);
String data = new BasicResponseHandler().handleResponse(response);
String base64 = android.util.Base64.encodeToString(data.getBytes("UTF-8"), android.util.Base64.DEFAULT);
webView.loadData(base64, "text/html; charset=utf-8", "base64");
This will fix all issues in webview rendering from android 1.5+
Will definitely work for you!
Upvotes: 0
Reputation: 105
This is a SDK Bug The loadData(String, ..., ...) method converts the content of the strings into a uri, and such that my code contains characters that needs to be encoded like % it truncated the code causing errors. so the solution as found here was to convert those characters to unicode as this code
public final static void webViewLoadData(WebView web, String html) {
StringBuilder buf = new StringBuilder(html.length());
for (char c : html.toCharArray()) {
switch (c) {
case '#': buf.append("%23"); break;
case '%': buf.append("%25"); break;
case '\'': buf.append("%27"); break;
case '?': buf.append("%3f"); break;
default:
buf.append(c); break;
}
}
web.loadData(buf.toString(), "text/html", "utf-8");
}
Upvotes: 2
Reputation: 6702
I've placed provided html file to assets folder with name snipt.html
. And this code displays it correctly.
final WebView webView = (WebView) findViewById(R.id.webview);
final WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/snipt.html");
Upvotes: 3