无法显示HTML字符串

时间:2015-03-23 11:22:07

标签: android python html webview

我正在努力使用Android WebView中的显示字符串HTML。 在服务器端,我下载了一个网页并转义HTML字符和引号(我使用了Python):

my_string = html.escape(my_string, True)

在Android客户端:字符串未转义为:

myString = StringEscapeUtils.unescapeHtml4(myString)
webview.loadData( myString, "text/html", "encoding");

然而,webview只是将它们显示为文字字符串。结果如下: enter image description here

编辑:我添加从服务器端返回的原始字符串:

  

"& lt;!DOCTYPE html& gt; &安培; LT; HTML   郎=安培; QUOT;恩&安培; QUOT;&安培; GT; &安培; LT; HEAD&安培; GT; &安培; LT;间   字符集=&安培; QUOT; UTF-8&安培; QUOT;&安培; GT; &安培; LT;间   HTTP的当量=安培;英寸×UA兼容&安培; QUOT;   含量=&安培; QUOT; IE =边缘&安培; QUOT;&安培; GT; &安培; LT;间   名称=安培; QUOT;视&安培; QUOT;   含量=&安培; QUOT;宽度=设备宽度,   初始规模= 1.0&安培; QUOT;&安培; GT; &安培; LT;间   名称=安培; QUOT;描述&安培; QUOT;   含量=&安培; QUOT;&安培; QUOT;&安培; GT;   &安培; LT;标题&安培; GT; Saulify&安培; LT; /标题&安培; GT; & lt;! - 所有的   Favicons ...... - & gt; & lt; link rel =& quot;快捷方式   图标&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/favicon.ico& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 57x57&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-57x57.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 114x114&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-114x114.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT;最高72x72&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-72x72.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 144x144&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-144x144.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 60×60&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-60x60.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 120×120&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-120x120.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 76x76&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-76x76.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 152x152&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-152x152.png& QUOT;&安培; GT;   & lt; link rel =& quot; apple-touch-icon& quot;   尺寸=安培; QUOT; 180x180&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/apple-touch-icon-180x180.png& QUOT;&安培; GT;   & lt; link rel =& quot; icon& quot;   类型=安培; QUOT;图像/ PNG&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/favicon-192x192.png& QUOT;   尺寸=安培; QUOT; 192x192&安培; QUOT;&安培; GT; &安培; LT;链接   的rel =&安培; QUOT;图标&安培; QUOT;类型=安培; QUOT;图像/ PNG&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/favicon-160x160.png& QUOT;   尺寸=安培; QUOT; 160x160的&安培; QUOT;&安培; GT; &安培; LT;链接   的rel =&安培; QUOT;图标&安培; QUOT;类型=安培; QUOT;图像/ PNG&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/favicon-96x96.png& QUOT;   尺寸=安培; QUOT; 96×96&安培; QUOT;&安培; GT; &安培; LT;链接   的rel =&安培; QUOT;图标&安培; QUOT;类型=安培; QUOT;图像/ PNG&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/favicon-16x16.png& QUOT;   尺寸=安培; QUOT 16×16安培; QUOT;&安培; GT; &安培; LT;链接   的rel =&安培; QUOT;图标&安培; QUOT;类型=安培; QUOT;图像/ PNG&安培; QUOT;   HREF =&安培; QUOT; /static/favicon/favicon-32x32.png& QUOT;   尺寸=安培; QUOT; 32×32&安培; QUOT;&安培; GT; &安培; LT;间   名称=安培; QUOT; msapplication-TileColor&安培; QUOT;   含量=&安培; QUOT;#da532c&安培; QUOT;&安培; GT; &安培; LT;间   名称=安培; QUOT; msapplication-TileImage&安培; QUOT;   含量=&安培; QUOT; /static/favicon/mstile-144x144.png& QUOT;&安培; GT;   & lt; meta name =& quot; msapplication-config& quot;   含量=&安培; QUOT; /static/favicon/browserconfig.xml& QUOT;&安培; GT;   & lt;! - 外部CSS - & gt; &安培; LT;链接   的rel =&安培; QUOT;样式表&安培; QUOT;   HREF =&安培; QUOT; https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">   & lt;! - 外部字体 - & gt; &安培; LT;链接   HREF =&安培; QUOT; // maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css"   的rel =&安培; QUOT;样式表&安培; QUOT;&安培; GT; &安培; LT;链接   HREF =&安培;#X27; // fonts.googleapis.com/css?family=Open+Sans:300,600'   的rel =&安培;#X27;样式表&安培;#X27;   类型=安培;#X27;文本/ CSS&安培;#X27;&安培; GT; &安培; LT;链接   HREF =&安培;#X27; // fonts.googleapis.com/css?family=Lora:400,700'   的rel =&安培;#X27;样式表&安培;#X27;   类型=安培;#X27;文本/ CSS&安培;#X27;&安培; GT; & lt;! - [如果是IE   9]&安培; GT; &安培; LT;脚本   SRC =&安培; QUOT; // cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script>   &安培; LT;脚本   SRC =&安培; QUOT; // cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>   &安培; LT; ENDIF] - 安培;!GT; & lt;! - 网站CSS - & gt;   & lt; link rel =& quot; stylesheet& quot;   类型=安培; QUOT;文本/ CSS&安培; QUOT;   HREF =&安培; QUOT; /static/css/style.css& QUOT;&安培; GT; &安培; LT;链接   的rel =&安培; QUOT;样式表&安培; QUOT;类型=安培; QUOT;文本/ CSS&安培; QUOT;   HREF =&安培; QUOT; /static/css/glyphicon.css& QUOT;&安培; GT;   &安培; LT /头&安培; GT; &安培; LT;身体安培; GT; &安培; LT; DIV   class =& quot; container article-page& quot;& gt; &安培; LT; DIV   类=安培; QUOT;排&安培; QUOT;&安培; GT; &安培; LT; DIV   class =& quot; col-md-8 col-md-offset-2& quot;& gt;   &安培; LT; H2&安培; GT;&安培; LT;一个   HREF =&安培; QUOT; http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html">Gov。   杰里布朗说泰德克鲁兹是& amp;#39;绝对   不宜和放大器;放大器;#39;因气候变化而竞选公职   观点&安培; LT; / A&安培; GT;&安培; LT / H2&安培; GT; &安培; LT; H4&安培; GT;萨姆   莱文&安培; LT / H4&安培; GT; &安培; LT; DIV   类=安培; QUOT;冠词安培; QUOT;&安培; GT; &安培; LT; P&安培; GT;加州   州长杰里·布朗(D)周日表示德州参议员特德克鲁兹(德克萨斯州)   是绝对不适合办公室& quot;   因为他对气候变化的立场。& lt; / p& gt;   & lt; p& gt;& quot;我刚从新罕布什尔州回来,在那里   那里到处都是#x27;雪和冰。我对此的看法很简单:   关于这一点的争论应遵循科学,并应遵循数据,以及许多   关于全球变暖的危言耸听者,他们有一个问题,因为   科学并没有将它们支持起来,& quot;克鲁兹< a   HREF =&安培; QUOT; https://www.youtube.com/watch?v=m0UJ_Sc0Udk">said</a>   与Seth Meyers& quot;& quot; Late Night持续   。星期&安培; LT; / P&安培; GT; & lt; p& gt;为了支持他的主张,克鲁兹   引用的卫星数据表明缺乏显着的变暖   过去17年。但Cruz' s推理& lt; a   HREF =&安培; QUOT; http://www.politifact.com/truth-o-meter/statements/2015/mar/20   /特德·克鲁兹/泰德 - cruzs - 世界 - 火 - 不过去的17年/&放大器; QUOT;&放大器; GT;有   被Politifact& lt; / a& gt;揭穿,已经证明了这一点   科学家们有足够的证据相信气候会   继续温暖。& lt; / p& gt; & lt; p& gt;& quot;他是什么   说完全是假的,“布朗说& lt; a   HREF =&安培; QUOT; http://www.nbcnews.com/meet-the-press/california-governor-ted-cruz-   不适宜待运行-n328046&安培; QUOT;&安培; GT; NBC&安培;#X27; S   & quot; Meet the Press。& quot;& lt; / a& gt;他补充说   &安培; LT;一个   HREF =&安培; QUOT; http://climate.nasa.gov/scientific-consensus/">over   90%& lt; / a& gt;研究气候的科学家们一致认同   气候变化是由人类活动引起的。 & quot;那个男人   如此无知和直接篡改   现有的科学数据。它'令人震惊,我想那个男人   让自己完全不适合参选   办公室,&安培; QUOT;布朗说。& lt; / p& gt; &安培; LT; P&安培; GT;布朗   并补充说,气候变化已经& lt; a   HREF =&安培; QUOT; http://www.huffingtonpost.com/2015/03/06/california-drought-february-   ?record_n_6820704.html utm_hp_ref =加州干旱&安培; QUOT;&安培; GT;引起   在他的州& lt; / a& gt;干旱,以及严重的寒冷和   东海岸的暴风雨。& lt; / p& gt; &安培; LT; P&安培; GT;而   数据显示,克鲁兹可能在新罕布什尔州到处都看到了冰雪   表明该国实际上正在经历一个& lt; a   HREF =&安培; QUOT; http://www.huffingtonpost.com/2015/02/19/cold-weather-   winter_n_6713104.html& quot;& gt;温暖比   平均&安培; LT; / A&安培; GT; 。冬季&安培; LT; / P&安培; GT;   & lt; p& gt;布朗对克鲁兹的批评来自前一天   德州参议员将宣布a& lt; a   HREF =&安培; QUOT; http://www.huffingtonpost.com/2015/03/22   /ted-cruz-2016_n_6917824.html">presidential   运动&安培; LT; / A&安培; GT ;. &安培; LT; / P&安培; GT; &安培; LT; / DIV&安培; GT;   & lt; div class =& quot; original& quot;& gt; &安培; LT;一个   HREF =&安培; QUOT; http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html">VIEW   ORIGINAL&安培; LT; / A&安培; GT; &安培; LT; / DIV&安培; GT; &安培; LT; / DIV&安培; GT;   &安培; LT; / DIV&安培; GT; &安培; LT; / DIV&安培; GT; &安培; LT;脚本   SRC =&安培; QUOT; // code.jquery.com/jquery-latest.js"></script>   &安培; LT;脚本   SRC =&安培; QUOT; /static/js/modal.js& QUOT;&安培; GT;&安培; LT; /脚本&安培; GT;   &安培; LT;脚本   SRC =&安培; QUOT; /static/js/bootbox.min.js& QUOT;&安培; GT;&安培; LT; /脚本&安培; GT;   &安培; LT;脚本   SRC =&安培; QUOT; /static/js/site.js& QUOT;&安培; GT;&安培; LT; /脚本&安培; GT; &安培; LT;脚本&安培; GT;   (功能(I,S,O,G,R,A,M){I [&安培;#X27; GoogleAnalyticsObject&安培;#X27;] = R; I [R] = I [R] ||函数(){   (ⅰ[R] .Q = I [R] .Q || [])。推(参数)},I [R] .L = 1 *新   日期(); A = s.createElement(○),   米= s.getElementsByTagName(O)[0]; a.async = 1; a.src =克; m.parentNode.insertBefore(A,M)   })(窗口,文件,&安培;#X27;脚本&安培;#X27;,&安培;#X27; // www.google-analytics.com/analytics.js','ga&#x27 );   ga(' create',' UA-56257533-1',   &安培;#X27;汽车和放大器;#X27;); GA(&安培;#X27;发送&安培;#X27 ;,   &安培;#X27;网页浏览&安培;#X27); &安培; LT; /脚本&安培; GT;   &安培; LT /体&安培; GT; &安培; LT; / HTML&安培; GT;"

3 个答案:

答案 0 :(得分:25)

我在这里修改了代码:

public class test extends Activity {
private WebView wv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    wv = (WebView) findViewById(R.id.wv);
    String s = "<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content=""> <title>Saulify</title> <!-- All the Favicons... --> <link rel="shortcut icon" href="/static/favicon/favicon.ico"> <link rel="apple-touch-icon" sizes="57x57" href="/static/favicon/apple-touch-icon-57x57.png"> <link rel="apple-touch-icon" sizes="114x114" href="/static/favicon/apple-touch-icon-114x114.png"> <link rel="apple-touch-icon" sizes="72x72" href="/static/favicon/apple-touch-icon-72x72.png"> <link rel="apple-touch-icon" sizes="144x144" href="/static/favicon/apple-touch-icon-144x144.png"> <link rel="apple-touch-icon" sizes="60x60" href="/static/favicon/apple-touch-icon-60x60.png"> <link rel="apple-touch-icon" sizes="120x120" href="/static/favicon/apple-touch-icon-120x120.png"> <link rel="apple-touch-icon" sizes="76x76" href="/static/favicon/apple-touch-icon-76x76.png"> <link rel="apple-touch-icon" sizes="152x152" href="/static/favicon/apple-touch-icon-152x152.png"> <link rel="apple-touch-icon" sizes="180x180" href="/static/favicon/apple-touch-icon-180x180.png"> <link rel="icon" type="image/png" href="/static/favicon/favicon-192x192.png" sizes="192x192"> <link rel="icon" type="image/png" href="/static/favicon/favicon-160x160.png" sizes="160x160"> <link rel="icon" type="image/png" href="/static/favicon/favicon-96x96.png" sizes="96x96"> <link rel="icon" type="image/png" href="/static/favicon/favicon-16x16.png" sizes="16x16"> <link rel="icon" type="image/png" href="/static/favicon/favicon-32x32.png" sizes="32x32"> <meta name="msapplication-TileColor" content="#da532c"> <meta name="msapplication-TileImage" content="/static/favicon/mstile-144x144.png"> <meta name="msapplication-config" content="/static/favicon/browserconfig.xml"> <!-- External CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> <!-- External Fonts --> <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"> <link href='//fonts.googleapis.com/css?family=Open+Sans:300,600' rel='stylesheet' type='text/css'> <link href='//fonts.googleapis.com/css?family=Lora:400,700' rel='stylesheet' type='text/css'> <!--[if lt IE 9]> <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script> <![endif]--> <!-- Site CSS --> <link rel="stylesheet" type="text/css" href="/static/css/style.css"> <link rel="stylesheet" type="text/css" href="/static/css/glyphicon.css"> </head> <body> <div class="container article-page"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <h2><a href="http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html">Gov. Jerry Brown Says Ted Cruz Is 'Absolutely Unfit' To Run For Office Because Of Climate Change Views</a></h2> <h4>Sam Levine</h4> <div class="article"> <p>California Gov. Jerry Brown (D) said on Sunday that Texas Sen. Ted Cruz (R-Texas) is "absolutely unfit to be running for office" because of his position on climate change.</p> <p>"I just came back from New Hampshire, where there's snow and ice everywhere. My view on this is simple: Debates on this should follow science and should follow data, and many of the alarmists on global warming, they have a problem because the science doesn't back them up," Cruz <a href="https://www.youtube.com/watch?v=m0UJ_Sc0Udk">said</a> on "Late Night with Seth Meyers" last week.</p> <p>To back up his claim, Cruz cited satellite data that has shown a lack of significant warming over the last 17 years. But Cruz's reasoning <a href="http://www.politifact.com/truth-o-meter/statements/2015/mar/20 /ted-cruz/ted-cruzs-worlds-fire-not-last-17-years/">has been debunked by Politifact</a>, which has shown that scientists have ample evidence to believe that the climate will continue to warm.</p> <p>"What he said is absolutely false,” Brown said on <a href="http://www.nbcnews.com/meet-the-press/california-governor-ted-cruz- unfit-be-running-n328046">NBC's "Meet the Press."</a> He added that <a href="http://climate.nasa.gov/scientific-consensus/">over 90 percent</a> of scientists who study the climate agree that climate change is caused by human activity. "That man betokens such a level of ignorance and a direct falsification of existing scientific data. It's shocking, and I think that man has rendered himself absolutely unfit to be running for office," Brown said.</p> <p>Brown added that climate change has <a href="http://www.huffingtonpost.com/2015/03/06/california-drought-february- record_n_6820704.html?utm_hp_ref=california-drought">caused droughts in his state</a>, as well as severe cold and storms on the east coast.</p> <p>While Cruz may have seen snow and ice everywhere in New Hampshire, data shows that the country is actually experiencing a <a href="http://www.huffingtonpost.com/2015/02/19/cold-weather- winter_n_6713104.html">warmer than average</a> winter.</p> <p>Brown’s criticism of Cruz comes one day before the Texas senator is set to announce a <a href="http://www.huffingtonpost.com/2015/03/22 /ted-cruz-2016_n_6917824.html">presidential campaign</a>. </p> </div> <div class="original"> <a href="http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html">VIEW ORIGINAL</a> </div> </div> </div> </div> <script src="//code.jquery.com/jquery-latest.js"></script> <script src="/static/js/modal.js"></script> <script src="/static/js/bootbox.min.js"></script> <script src="/static/js/site.js"></script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-56257533-1', 'auto'); ga('send', 'pageview'); </script> </body> </html>";


    wv.loadData(stripHtml(s), "text/html", "UTF-8");

}

public String stripHtml(String html) {
    return Html.fromHtml(html).toString();
}

}

enter image description here

答案 1 :(得分:5)

试试这段代码,

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N){
   yourtextview.setText(Html.fromHtml(yourstring,Html.FROM_HTML_MODE_LEGACY));
}
else {
   yourtextview.setText(Html.fromHtml(yourstring));
}

答案 2 :(得分:4)

试试这个:

wv = (WebView) findViewById(R.id.wv);
String s = "You HTML string";
wv.loadData(stripHtml(s), "text/html", "UTF-8");

public String stripHtml(String html) {
    return Html.fromHtml(html).toString();
}