Impossibile visualizzare la stringa HTML

Mar 23 2015

Sto lottando con la stringa di visualizzazione HTML in Android WebView.

Sul lato server, ho scaricato una pagina web ed esegui l'escape di caratteri HTML e virgolette (ho usato Python):

my_string = html.escape(my_string, True)

Sul lato client Android: le stringhe sono senza caratteri di escape:

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

Tuttavia webview li visualizza solo come stringhe letterali. Ecco il risultato:

Modifica: aggiungo la stringa originale restituita dal lato server:

"& 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;! - Tutte le favicon ... - & gt; & lt; link rel = & quot; shortcut icon & quot; href = & quot; / static / favicon / favicon. ico & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot ;izes = & quot; 57x57 & quot; href = & quot; /static/favicon/apple-touch-icon-57x57.png" & gt; & lt; link rel = & quot ; apple-touch-icon & quot ;izes = & quot; 114x114 & quot; href = & quot; /static/favicon/apple-touch-icon-114x114.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; sizes = & quot ; 72x72 & quot; href = & quot; /static/favicon/apple-touch-icon-72x72.png" & 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; dimensioni = & quot; 60 x 60 & quot; href = & quot; /static/favicon/apple-touch-icon-60x60.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 120x120 & quot; href = & quot; /static/favicon/apple-touch-icon-120x120.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 76 x 76 & quot; href = & quot; /static/favicon/apple-touch-icon-76x76.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 152 x 152 & quot; href = & quot; /static/favicon/apple-touch-icon-152x152.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 180x180 & quot; href = & quot; /static/favicon/apple-touch-icon-180x180.png" & gt; & lt; link rel = & quot; icon & quot; type = & quot; image / png & quot; href = & quot; /static/favicon/favicon-192x192.png" dimensioni = & quot; 192 x 192 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image / png & quot; href = & quot; /static/favicon/favicon-160x160.png" dimensioni = & quot; 160x160 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image / png & quot; href = & quot; /static/favicon/favicon-96x96.png" dimensioni = & quot; 96 x 96 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image / png & quot; href = & quot; /static/favicon/favicon-16x16.png" dimensioni = & quot; 16 x 16 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image / png & quot; href = & quot; /static/favicon/favicon-32x32.png" dimensioni = & quot; 32 x 32 & 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" & gt; & lt; meta name = & quot; msapplication-config & quot; content = & quot; /static/favicon/browserconfig.xml" & gt; & lt;! - CSS esterno - & gt; & lt; link rel = & quot; foglio di stile & quot; href = & quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css & quot; & gt; & lt;! - Caratteri esterni - & gt; & lt; link href = & quot; // maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel = & quot; foglio di stile & quot; & gt; & lt; link href = & # x27; // fonts.googleapis.com/css?family=Open+Sans:300,600' rel = & # x27; foglio di stile & # x27; tipo = & # x27; testo / css & # x27; & gt; & lt; link href = & # x27; // fonts.googleapis.com/css?family=Lora:400,700' rel = & # x27; foglio di stile & # x27; tipo = & # 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"></script> & lt; script src = & quot; // cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script> & lt;! [endif] - & gt; & lt;! - CSS sito - & gt; & lt; link rel = & quot; foglio di stile & quot; tipo = & quot; text / css & quot; href = & quot; /static/css/style.css" & gt; & lt; link rel = & quot; foglio di stile & quot; tipo = & quot; text / css & quot; href = & quot; /static/css/glyphicon.css" & 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”6919002.html & quot; & gt; Gov. Jerry Brown dice che Ted Cruz è & amp; # 39; assolutamente inadatto & amp; # 39; Per candidarsi a un ufficio a causa del cambiamento climatico Viste & lt; / a & gt; & lt; / h2 & gt; & lt; h4 & gt; Sam Levine & lt; / h4 & gt; & lt; div class = & quot; article & quot; & gt; & lt; p & gt; Il governatore della California Jerry Brown (D) ha detto domenica che il senatore del Texas Ted Cruz (R-Texas) è "assolutamente inadatto a candidarsi per una carica". a causa della sua posizione sul cambiamento climatico. & lt; / p & gt; & lt; p & gt; "Sono appena tornato dal New Hampshire, dove ci sono neve e ghiaccio ovunque. La mia opinione su questo è semplice: i dibattiti su questo dovrebbero seguire la scienza e dovrebbero seguire i dati, e molti degli allarmisti sul riscaldamento globale hanno un problema perché la scienza non li sostiene, & quot; Cruz & lt; a href = & quot; https://www.youtube.com/watch?v=m0UJ_Sc0Udk & quot; & gt; said & lt; / a & gt; in & quot; Late Night with Seth Meyers & quot; la scorsa settimana. & lt; / p & gt; & lt; p & gt; A sostegno della sua affermazione, Cruz ha citato dati satellitari che hanno mostrato una mancanza di riscaldamento significativo negli ultimi 17 anni. Ma il ragionamento di Cruz & 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; è stato smentito da Politifact & lt; / a & gt ;, che ha dimostrato che gli scienziati hanno ampie prove per ritenere che il clima continuerà a riscaldarsi. & lt; / p & gt; & lt; p & gt; & quot; Quello che ha detto è assolutamente falso ", ha detto Brown su & 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; Ha aggiunto che & lt; a href = & quot; http://climate.nasa.gov/scientific-consensus/ & quot; & gt; oltre il 90 percento & lt; / a & gt; degli scienziati che studiano il clima concorda sul fatto che il cambiamento climatico è causato dall'attività umana. "Quell'uomo indica un tale livello di ignoranza e una falsificazione diretta dei dati scientifici esistenti. È scioccante e penso che l'uomo si sia reso assolutamente inadatto a candidarsi per una carica "," Brown ha detto. & Lt; / p & gt; & lt; p & gt; Brown ha aggiunto che il cambiamento climatico ha & lt; a href = & quot; http://www.huffingtonpost.com/2015/03/06/california-drought-february- record wants6820704.html? utm_hp_ref = california-siccità & quot; & gt; ha causato siccità nel suo stato & lt; / a & gt ;, oltre a forti raffreddori e tempeste sulla costa orientale. & lt; / p & gt; & lt; p & gt; Anche se Cruz può aver visto neve e ghiaccio ovunque nel New Hampshire, i dati mostrano che il paese sta effettivamente vivendo un & lt; a href = & quot; http://www.huffingtonpost.com/2015/02/19/cold-weather- winter”6713104.html & quot; & gt; più caldo della media & lt; / a & gt; inverno. & lt; / p & gt; & lt; p & gt; Le critiche di Brown nei confronti di Cruz arrivano un giorno prima che il senatore del Texas annuncerà un & lt; a href = & quot; http://www.huffingtonpost.com/2015/03/22 / ted-cruz-2016” valigia6917824.html"> campagna presidenziale & 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”6919002.html& quot; & gt; VISUALIZZA ORIGINALE & 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"></script> & lt; script src = & quot; /static/js/modal.js" & gt; & lt; / script & gt; & lt; script src = & quot; /static/js/bootbox.min.js" & gt; & lt; / script & gt; & lt; script src = & quot; /static/js/site.js" & 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 (argomenti)}, 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)}) (finestra, documento, & # x27; script & # x27;, & # x27; //www.google-analytics.com/analytics.js','ga'); 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; "

Risposte

57 MrDumb Mar 23 2015 at 18:24

Ho modificato il codice qui:

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

}

11 FeriAgusetiawan Mar 25 2018 at 16:06

Prova questo codice,

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));
}
8 Noname Mar 31 2016 at 19:21

Prova questo:

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