Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法显示HTML字符串_Android_Python_Html_Webview - Fatal编程技术网

Android 无法显示HTML字符串

Android 无法显示HTML字符串,android,python,html,webview,Android,Python,Html,Webview,我正在Android WebView中努力使用显示字符串HTML 在服务器端,我下载了一个网页并转义HTML字符和引号(我使用Python): 在Android客户端:字符串由以下内容取消替换: myString = StringEscapeUtils.unescapeHtml4(myString) webview.loadData( myString, "text/html", "encoding"); 但是webview只是将它们显示为文本字符串。结

我正在Android WebView中努力使用显示字符串HTML

在服务器端,我下载了一个网页并转义HTML字符和引号(我使用Python):

在Android客户端:字符串由以下内容取消替换:

myString = StringEscapeUtils.unescapeHtml4(myString)
webview.loadData( myString, "text/html", "encoding");
但是webview只是将它们显示为文本字符串。结果如下:

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

“!DOCTYPE html lang=“en”头元 charset=“utf-8”元 http equiv=“X-UA-Compatible” content=“IE=edge”元 name=“视口” content=“width=设备宽度, 初始刻度=1.0“meta name=“description” content=“” titleSaulify/标题!——所有 Favicons…--link rel=“快捷方式 “图标” href=“/static/favicon/favicon.ico” link rel=“苹果触摸图标” size=“57x57” href=“/static/favicon/apple-touch-icon-57x57.png” link rel=“苹果触摸图标” 尺寸=“114x114” href=“/static/favicon/apple-touch-icon-114x114.png” link rel=“苹果触摸图标” 尺寸=“72x72” href=“/static/favicon/apple-touch-icon-72x72.png” link rel=“苹果触摸图标” 尺寸=“144x144” href=“/static/favicon/apple-touch-icon-144x144.png” link rel=“苹果触摸图标” 尺寸=“60x60” href=“/static/favicon/apple-touch-icon-60x60.png” link rel=“苹果触摸图标” 尺寸=“120x120” href=“/static/favicon/apple-touch-icon-120x120.png” link rel=“苹果触摸图标” 尺寸=“76x76” href=“/static/favicon/apple-touch-icon-76x76.png” link rel=“苹果触摸图标” size=“152x152” href=“/static/favicon/apple-touch-icon-152x152.png” link rel=“苹果触摸图标” 尺寸=“180x180” href=“/static/favicon/apple-touch-icon-180x180.png” link rel=“图标” type=“image/png” href=“/static/favicon/favicon-192x192.png” size=“192x192”链接 rel=“icon”type=“image/png” href=“/static/favicon/favicon-160x160.png” size=“160x160”链接 rel=“icon”type=“image/png” href=“/static/favicon/favicon-96x96.png” size=“96x96”链接 rel=“icon”type=“image/png” href=“/static/favicon/favicon-16x16.png” size=“16x16”链接 rel=“icon”type=“image/png” href=“/static/favicon/favicon-32x32.png” 尺寸=“32x32”元 name=“msapplication TileColor” content=“#da532c”元 name=“msapplication TileImage” content=“/static/favicon/mstile-144x144.png” meta name=“msapplication配置” content=“/static/favicon/browserconfig.xml” !--外部CSS--链接 rel=“样式表” href=“” !--外部字体--链接 href=“//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css” rel=“样式表”链接 href=';//font.googleapis.com/css?family=Open+Sans:300600'; rel=';样式表'; type=';文本/css';链接 href=';//font.googleapis.com/css?family=Lora:400700'; rel=';样式表'; type=';text/css';!--[if lt IE 9] 剧本 src=“//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js”/script 剧本 src=“//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js”/script ![endif]-!--站点CSS-- link rel=“样式表” type=“text/css” href=“/static/css/style.css”链接 rel=“stylesheet”type=“text/css” href=“/static/css/glyphicon.css” /头身部 class=“容器文章页”div class=“行”div class=“col-md-8 col-md-offset-2” h2a href=”“Gov.hk。 杰瑞·布朗说特德·克鲁兹绝对是 因气候变化而不适合竞选公职 视图/a/h2 h4Sam 莱文/h4分区 class=“article”pCalifornia 州长杰里·布朗(D)周日表示,德克萨斯州参议员特德·克鲁兹(R-Texas) “绝对不适合竞选公职” 因为他在气候变化问题上的立场。/p 我刚从新罕布什尔州回来,那里 那里';到处都是冰雪。我的看法很简单: 关于这一点的辩论应该遵循科学,应该遵循数据,还有很多 在全球变暖的危言耸听者中,他们有一个问题,因为 科学没有';“不要支持他们,”克鲁兹说 href=”“said/a 在昨晚的“塞思·迈尔斯深夜”节目中 周/p pTo支持他的要求,克鲁兹 引用的卫星数据显示,全球变暖幅度不大 过去的17年。但克鲁兹的推理 href=” /ted cruz/ted-cruzs-worlds-fire-not-last-17年/“已经 被政治行动/a揭穿,它表明 科学家们有充分的证据相信气候会改变 继续加热。/p“他做了什么 布朗在一次采访中说:“我说的绝对是假的。” href=” 不适合运行-n328046“NBC';s “与媒体见面。”/a他补充道 A. href=”“结束 90%研究气候的科学家同意这一观点 气候变化是由人类活动引起的。“那个人 预示着这种无知程度和直接伪造 现有的科学数据。这太令人震惊了,我认为那个人 使自己完全不适合竞选 布朗说 他补充说,气候变化有一个重要的影响 href=” 记录\n\u 6820704.html?utm\u hp\u ref=加州干旱“造成的 他所在州的干旱/a以及严寒和干旱 东海岸的风暴。/p一会儿 数据显示,克鲁兹可能在新罕布什尔州到处都能看到冰雪 表明该国实际上正在经历一场灾难 href=” winter_n_6713104.html“比 平均每年冬季/p pBrown对克鲁兹的批评是在审判前一天提出的 德克萨斯州参议员将宣布一项新的选举 href=” /ted-cruz-2016_n_6917824.html“总统 运动/a/p/分区 div class=“原件”a href=”“视图 原件 /div/div脚本 src=“//code.jquery.com/jquery latest.js”/script 剧本 src=“/static/js/modal.js”/script 剧本 src=“/static/js/bootbox.min.js”/script 剧本 src=“/static/js/site.js”/script脚本 (函数(i,s,o,g,r,a,m){i[';GoogleAnalyticsObject';]=r;i[r]=i[r]|函数(){ (i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新 日期();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(窗口、文档和脚本';';//www.google-analytics.com/analytics.js';';ga';); ga(';创建';,';UA-56257533-1';, &#自动';);ga(';发送';, &#页面视图';)/剧本 /正文/html“


我在这里修改了代码:

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();
}
试试这个代码

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));
}
问题见
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));
}