Android-Droid
Android-Droid

Reputation: 14585

Android WebView not loading local html string correctly

I'm working on a project where I'm receiving some html string which I should show in WebView. The problem is that the html string contains only body part, without any tags like <html>, <head>, <body> and when I'm trying to show the received string in webView the result is showing the tags included in html string received from server.

Here is some example what I receive and what I'm doing :

Html String received from server :

05-30 14:02:56.785: D/(16970): body : &lt;p align=&#039;justify&#039;&gt;The so-called Fiscal Compact is unlikely to be approved by the Latvian parliament in the second and final reading as four opposition MPs said that they would not back the initiative, local media reported on Tuesday evening. The second reading is scheduled for May 31. Under local legislation, the bill should be backed by 67 MPs in the 100-seat parliament, though the ruling coalition controls only 56 seats and depends on the opposition Union of Greens and Farmers (ZZS) to approve it. During the first reading 68 MPs backed the bill. The four MPs from ZZS will meet PM Dombrovskis later today to discuss the issue. They want more clarity about the country&rsquo;s participation in neighboring Lithuania&rsquo;s nuclear power plant project. Previously, ZZS said it extends its support on condition that the government fights for higher subsidies for farmers in the 2014-2020 EU budget framework.&lt;/p&gt;

Here is my code :

String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + bodyTxt + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);

and the result is this :

result

I've tried using body.loadData(newBody, "text/html", "UTF-8"); , but this way didn't even show me the result.

So any ideas which I'm doing wrong and how can I fix this?

Thanks in advance!

Upvotes: 3

Views: 5753

Answers (3)

Ashok
Ashok

Reputation: 601

you can use this

webview.loadData(Html.fromHtml(htmlData),"text/html","utf-8"); 

Upvotes: 1

Android-Droid
Android-Droid

Reputation: 14585

As I can see from your logs you are receiving the html tags not with < symbol,but like &gt;. I guess if you replace these characters in your html string it will work as it should be. Try this :

        String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
        String replaced = bodyTxt.replace("&lt;","<");
        Log.d("","replaced : "+replaced);
        String replaced2 = replaced.replace("&gt;", ">");
        Log.d("","replaced2 : "+replaced2);
        Log.d("","body : "+bodyTxt);
        String newBody = "<html><body>" + replaced2 + "</body></html>";
        body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);

Upvotes: 1

Daniel  Magnusson
Daniel Magnusson

Reputation: 9674

This is how i do it;

String htmlContent = "<html><body>hello</body></html>";
WebView wv = (WebView) findViewById(R.id.my_view);
wv.loadData(htmlContent, "text/html", "UTF-8");

Investigate how the data looks in the db, all the "<>" and such are quoted with "<", this might be the problem you looking for.

Upvotes: 0

Related Questions