3

我正在做一个项目,我正在接收一些我应该在WebView. 问题是 html 字符串仅包含正文部分,没有任何标签<html>, <head>, <body>,当我尝试在 webView 中显示接收到的字符串时,结果显示从服务器接收到的 html 字符串中包含的标签。

这是我收到的和正在做的一些例子:

从服务器收到的 Html 字符串:

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;

这是我的代码:

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);

结果是这样的:

结果

我试过使用body.loadData(newBody, "text/html", "UTF-8"); ,但这种方式甚至没有显示结果。

那么我做错了什么想法,我该如何解决这个问题?

提前致谢!

4

3 回答 3

1

正如我从您的日志中看到的那样,您收到的 html 标签不是带有<符号,而是像&gt;. 我想如果你在你的 html 字符串中替换这些字符,它会正常工作。试试这个 :

        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);
于 2012-05-30T15:47:35.100 回答
1

你可以用这个

webview.loadData(Html.fromHtml(htmlData),"text/html","utf-8"); 
于 2012-10-25T14:13:50.713 回答
0

我就是这样做的;

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

调查数据在数据库中的外观,所有“<>”等都用“<”引用,这可能是您要寻找的问题。

于 2012-05-30T11:21:35.717 回答