无法显示HTML字符串

Hoa*_* Vu 67 html python android webview

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

my_string = html.escape(my_string, True)
Run Code Online (Sandbox Code Playgroud)

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

myString = StringEscapeUtils.unescapeHtml4(myString)
webview.loadData( myString, "text/html", "encoding");
Run Code Online (Sandbox Code Playgroud)

但是webview只是将它们显示为文字字符串.结果如下: 在此输入图像描述

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

"<!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><! - 所有Favicons ... - >< link rel ="快捷图标" 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 /图标/苹果 点触摸图标-144x144.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 60×60" HREF =" /static/favicon/apple-touch-icon-60x60.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 120×120" HREF =" /static/favicon/apple-touch-icon-120x120.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 76x76" HREF =" /static/favicon/apple-touch-icon-76x76.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 152x152" HREF =" /static/favicon/apple-touch-icon-152x152.png"> < link rel =" apple-touch-icon" 尺寸= QUOT; 180x180" HREF =" /static/favicon/apple-touch-icon-180x180.png"> < link rel =" icon" 类型= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-192x192.png" 尺寸= QUOT; 192x192"> < link rel =" icon" 类型= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-160x160.png" 尺寸= QUOT; 160x160的"> < link rel =" icon" 类型= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-96x96.png" 尺寸= QUOT; 96×96"> < link rel =" icon" 类型= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-16x16.png" 尺寸= QUOT 16×16 QUOT;> < link rel =" icon" 类型= QUOT;图像/ PNG" HREF =" /static/favicon/favicon-32x32.png" 尺寸= QUOT; 32×32"> < meta name =" msapplication-TileColor" 含量="#da532c"> < meta name =" msapplication-TileImage" 含量=" /static/favicon/mstile-144x144.png"> < meta name =" msapplication-config" 含量=" /static/favicon/browserconfig.xml"> <! - 外部CSS - > < link rel =" stylesheet" HREF =" https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> ; <! - 外部字体 - > < 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 ='样式表' 类型='文本/ CSS'> < link href =' // fonts.googleapis.com/css?family=Lora:400,700' 的rel ='样式表' 类型='文本/ 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!; <! - Site CSS - > < link rel =" stylesheet" 类型= QUOT;文本/ CSS" HREF =" /static/css/style.css"> < link rel =" stylesheet" 类型= 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">Gov.杰里布朗说泰德克鲁兹是&#39;绝对不合适&#39; 因气候变化观点而竞选办公室</a></h2> < h4> Sam Levine</h4> < div class =" article"> < p>加利福尼亚州州长杰里·布朗(D)周日表示,得克萨斯州参议员特德克鲁兹(R-Texas)"绝对不适合竞选公职".因为他对气候变化的立场.</p> < p>"我刚从新罕布什尔州回来,那里到处都是雪和冰.我对此的看法很简单:关于这一点的争论应该遵循科学,应该遵循数据,而且许多关于全球变暖的危言耸听,他们都有问题,因为科学并没有支持它们," Cruz< a href =" https://www.youtube.com/watch?v=m0UJ_S​​c0Udk">said</a> ; 在"Seth Meyers的深夜"中 上周.</p> < p>为了支持他的说法,克鲁兹引用的卫星数据表明,在过去的17年中,这种数据并没有显着升温.但克鲁兹' s推理< a href =" http://www.politifact.com/truth-o-meter/statements/2015/mar/20 /ted-cruz/ted-cruzs-worlds-fire-not-last-17-years /">由Politifact</a>揭穿,这表明科学家有足够的证据相信气候会继续变暖.</p> < p>"他所说的绝对是假的,"布朗说< a href =" http://www.nbcnews.com/meet-the-press/california-governor-ted-cruz- unfit-be-running-n328046"> NBC' s" Meet the Press."< /一个> 他补充说< a href =" http://climate.nasa.gov/scientific-consensus/">over 90%</a> 研究气候的科学家认为气候变化是由人类活动引起的."那个人对这种无知程度和对现有科学数据的直接篡改有所了解.它令人震惊,我认为那个人已经让自己完全不适合竞选公职," 布朗说.</p> < p> Brown补充说,气候变化已经< a href =" http://www.huffingtonpost.com/2015/03/06/california-drought-february- record_n_6820704.html?utm_hp_ref = california-drought">在他的州</a>造成干旱,以及严寒和东海岸的暴风雨.</p> < p>虽然克鲁兹可能在新罕布什尔州到处都看到了冰雪,但数据显示该国实际上正在经历一场< a href =" http://www.huffingtonpost.com/2015/02/19/cold-weather- winter_n_6713104.html">比平均温度更高</a> .冬季</p为H. < p>布朗对克鲁兹的批评发生在德克萨斯州参议员宣布< a href ="之前一天.http://www.huffingtonpost.com/2015/03/22 /ted-cruz-2016_n_6917824.html">presidential campaign</a>.</p为H. </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> <脚本> (函数(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'); < /脚本> < /体> </HTML>"中

MrD*_*umb 35

我在这里修改了代码:

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


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

    }

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

}
Run Code Online (Sandbox Code Playgroud)

}

在此输入图像描述


小智 6

尝试这个:

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();
}
Run Code Online (Sandbox Code Playgroud)


小智 6

试试这个代码,

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));
}
Run Code Online (Sandbox Code Playgroud)