无法在webview中显示htmlstring

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

my_string = html.escape(my_string, True) 

在Android客户端:string没有被转义:

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

但是,webview只是将它们显示为文字string。 结果如下: 在这里输入图像说明

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

<><!DOCTYPE html>< html lang =" en">< head>< meta charset =" utf-8">< meta http-equiv =" X-UA-Compatible" content =“width = device-width,initial-scale = 1.0”>< meta name =" description"内容=""元名称=" viewport" ;>< title> Saulify< / title><! – 所有Favicons … – >< link rel ="快捷图标" href =" / static / favicon / favicon。 < link rel =" apple-touch-icon" sizes =" 57×57" href =" /static/favicon/apple-touch-icon-57×57.png">< link rel ="" ; apple-touch-icon“sizes =”114×114“href =”/static/favicon/apple-touch-icon-114×114.png“>< link rel =" apple-touch-icon" sizes =" ; / rel =“apple-touch-icon”size =“144×144”href =“/ static /图标/苹果 点触摸图标-144×144.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 60×60" HREF =" /static/favicon/apple-touch-icon-60×60.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 120×120" HREF =" /static/favicon/apple-touch-icon-120×120.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 76×76" HREF =" /static/favicon/apple-touch-icon-76×76.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 152×152" HREF =" /static/favicon/apple-touch-icon-152×152.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 180×180" HREF =" /static/favicon/apple-touch-icon-180×180.png"> < link rel ="图标" types= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-192×192.png" 尺寸= QUOT; 192×192"> < link rel ="图标" types= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-160×160.png" 尺寸= QUOT; 160×160的"> < link rel ="图标" types= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-96×96.png" 尺寸= QUOT; 96×96"> < link rel ="图标" types= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-16×16.png" 尺寸= QUOT 16×16 QUOT;> < link rel ="图标" types= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-32×32.png" 尺寸= QUOT; 32×32"> < meta name =" msapplication-TileColor" 含量="#da532c"> < meta name =" msapplication-TileImage" 含量=" /static/favicon/mstile-144×144.png"> < meta name =" msapplication-config" 含量=" /static/favicon/browserconfig.xml"> <! – 外部CSS – > < link rel ="样式表" HREF =" https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"&gt ; <! – 外部字体 – > < link href =" // maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" 的rel ="样式表"> < link href =' // fonts.googleapis.com/css?family=Open+Sans:300,600' 的rel ='样式表' types='文本/ CSS'> < link href =' // fonts.googleapis.com/css?family=Lora:400,700' 的rel ='样式表' types='文本/ 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] – GT!; <! – 网站CSS – > < link rel ="样式表" types= QUOT;文本/ CSS" HREF =" /static/css/style.css"> < link rel ="样式表" types= QUOT;文本/ CSS" HREF =" /static/css/glyphicon.css"> < /头> <身体GT; < 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">政府 。 杰瑞·布朗说泰德·克鲁兹“绝对不适合” 由于气候变化的影响而奔赴办公室< / a>< / h2> < h4> Sam Levine< / h4> < div class =" article"> 加利福尼亚州州长杰里·布朗(Jerry Brown)周日表示,得克萨斯州参议员泰德·克鲁兹(Ted Cruz)(R-Texas)“绝对不适合竞选办公室” 因为他对气候变化的立场。< / p> < p>“我刚刚从新罕布什尔州回来,那里到处都是冰雪。 我对此的看法很简单:关于这个问题的辩论应该遵循科学,应该遵循数据和许多关于全球变暖的危言耸听,他们有一个问题,因为科学不支持它们, 克鲁兹< a href =" https://www.youtube.com/watch?v=m0UJ_S​​c0Udk">said</a&gt ; 与“Seth Meyers的深夜” 上周。< / p> &nbsp            但克鲁兹的推理< a href =" http://www.politifact.com/truth-o-meter/statements/2015/mar/20 / ted-cruz / ted-cruzs-worlds-fire-not-last-17-years /“>”已经揭露了政治事件< / a>,这表明科学家有充分的证据相信,气候将持续升温。< / p>              http://www.nbcnews.com/meet-the-press/california-governor-ted-cruz-freit-be-running-n328046“>&gt ; NBC' s”见面新闻“< /一个> 他补充说,< a href =" http://climate.nasa.gov/scientific-consensus/"&gt ; over 90 percent< / a> 研究气候的科学家认为,气候变化是由人类活动引起的。 “那个人认为这样的无知程度和直接伪造现有的科学数据。 这令人震惊,我认为这个人已经使自己绝对不适合竞选公职, 布朗说。< / p> Brown补充说,气候变化有一个“ http://www.huffingtonpost.com/2015/03/06/california-drought-february- record_n_6820704.html?utm_hp_ref =加利福尼亚州 – 干旱“>在他的州造成了干旱< / a>,以及严重的寒冷和东海岸的风暴。< / p> 虽然克鲁兹可能在新罕布什尔州的任何地方都看到了冰雪,但数据显示,该国实际上正在经历一场“寒冬”。 http://www.huffingtonpost.com/2015/02/19/cold-weather- winter_n_6713104.html“>比平均温暖< / a> 。冬季< / p为H. 布朗对克鲁兹的批评来了一天,得克萨斯州参议员宣布一个“ http://www.huffingtonpost.com/2015/03/22 /ted-cruz-2016_n_6917824.html">总统广告系列< / a&gt ;. < / p为H. < / DIV> < div class =" original"> < a href =" http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html"&gt ; 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> <脚本> (函数(i,s,o,g,r,a,m){i [&& xA; GoogleAnalyticsObject []] = r; i [r] = i [r] || function (),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&#x27); (' create'' UA-56257533-1'' auto'); ga(' send'' pageview'); < /脚本> < /体> < / HTML>”中

Solutions Collecting From Web of "无法在webview中显示htmlstring"

我在这里修改了代码:

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

}

在这里输入图像说明

尝试这个:

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