2015-03-23 13 views
12

Sono in difficoltà con la stringa di visualizzazione HTML in Android WebView. Sul lato server, ho scaricato una pagina web e caratteri di escape HTML e citazioni (io ho usato Python):Impossibile visualizzare la stringa HTML

my_string = html.escape(my_string, True) 

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

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

Tuttavia WebView basta visualizzarli come stringhe letterali. Ecco il risultato: enter image description here

Edit: aggiungo stringa originale tornato 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 nome = & quot; viewport & quot; content = & quot; width = device-width, initial-scale = 1.0 & quot; & gt; & lt; meta nome = & quot; descrizione & quot; contenuto = & quot; & quot; & gt; & lt; titolo & gt; Saulify & lt;/title & gt; & lt;! - Tutte le Favicon ... - & gt; & lt; link rel = & quot; collegamento icona & quot; href = & quot; /static/favicon/favicon.ico & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 57x57 & quot; href = & quot; /static/favicon/apple-touch-icon-57x57.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 114x114 & quot; href = & quot; /static/favicon/apple-touch-icon-114x114.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 72x72 & quot; href = & quot;/static/favicon/apple-touch-icon-72x72.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 144x144 & quot; href = & quot; /static/favicon/apple-touch-icon-144x144.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 60x60 & quot; href = & quot; /static/favicon/apple-touch-icon-60x60.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 120x120 & quot; href = & quot; /static/favicon/apple-touch-icon-120x120.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 76x76 & quot; href = & quot; /static/favicon/apple-touch-icon-76x76.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 152x152 & quot; href = & quot; /static/favicon/apple-touch-icon-152x152.png & quot; & gt; & lt; link rel = & quot; apple-touch-icon & quot; dimensioni = & quot; 180x180 & quot; href = & quot; /static/favicon/apple-touch-icon-180x180.png & quot; & gt; & lt; link rel = & quot; icon & quot; tipo = & quot; image/png & quot; href = & quot; /static/favicon/favicon-192x192.png & quot; dimensioni = & quot; 192x192 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image/png & quot; href = & quot; /static/favicon/favicon-160x160.png & quot; dimensioni = & quot; 160x160 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image/png & quot; href = & quot;/static/favicon/favicon-96x96.png & quot; dimensioni = & quot; 96x96 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image/png & quot; href = & quot; /static/favicon/favicon-16x16.png & quot; dimensioni = & quot; 16x16 & quot; & gt; & lt; link rel = & quot; icon & quot; type = & quot; image/png & quot; href = & quot; /static/favicon/favicon-32x32.png & quot; dimensioni = & quot; 32x32 & quot; & gt; & lt; meta nome = & quot; msapplication-TileColor & quot; contenuto = & quot; # da532c & quot; & gt; & lt; meta nome = & quot; msapplication-TileImage & quot; contenuto = & quot; /static/favicon/mstile-144x144.png & quot; & gt; & lt; meta name = & quot; msapplication-config & quot; contenuto = & quot; /static/favicon/browserconfig.xml & quot; & 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"> & lt;! - Font esterni - & gt; & lt; collegamento href = & quot; // maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css & quot; rel = & quot; foglio di stile & quot; & gt; & lt; collegamento href = & # x27; // fonts.googleapis.com/css?family=Open+Sans:300,600 & # x27; rel = & # x27; foglio di stile & # x27; tipo = & # x27; testo/css & # x27; & gt; & lt; link href = & # x27; // fonts.googleapis.com/css?family=Lora:400,700 & # x27; rel = & # x27; foglio di stile & # x27; tipo = & # x27; testo/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; foglio di stile & quot; tipo = & quot; testo/css & quot; href = & quot; /static/css/style.css & quot; & gt; & lt; link rel = & quot; foglio di stile & quot; type = & quot; text/css & quot; href = & quot; /static/css/glyphicon.css & quot; & gt; & lt;/testa & gt; & lt; corpo & gt; & lt; div classe = & quot; contenitore articolo-pagina & quot; & gt; & lt; div classe = & quot; riga & quot; & gt; & lt; div classe = & 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">Gov. Jerry Brown dice Ted Cruz Is & amp; # 39; Assolutamente Unfit & amp; # 39; Per correre per ufficio a causa del cambiamento climatico Visualizzazioni & lt;/a & gt; & lt;/h2 & gt; & lt; h4 & gt; Sam Levine & lt;/h4 & gt; & lt; div classe = & quot; articolo & quot; & gt; & lt; p & gt; California governatore Jerry Brown (D) ha detto di Domenica che il Texas senatore Ted Cruz (R-Texas) è & quot; assolutamente inadatto a essere in esecuzione per l'ufficio & quot; a causa della sua posizione sul cambiamento climatico. & lt;/p & gt; & lt; p & gt; & quot; Sono appena tornato dal New Hampshire, dove # x27, & c'è neve e ghiaccio ovunque. Il mio punto di vista 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 esegue il backup & # x27; t & quot; Cruz & lt; a href = & quot; https://www.youtube.com/watch?v=m0UJ_Sc0Udk">said</a> su & quot; Late Night con Seth Meyers & quot; ultima settimana . & lt;/p & gt; & lt; p & gt; Per eseguire il backup del suo reclamo, i dati relativi ai satelliti di Cruz hanno evidenziato una mancanza di riscaldamento significativo rispetto agli negli ultimi 17 anni.Ma Cruz & # x27; s ragionamento & 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 ridimensionato da Politifact & lt;/a & gt ;, che ha dimostrato che scienziati hanno ampie prove per credere che il clima sarà 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/">over 90 percento & lt;/a & gt; di scienziati che studiano il clima sono d'accordo che il cambiamento climatico è causato dall'attività umana. & quot; Quell'uomo indica un tale livello di ignoranza e una falsificazione diretta dei dati scientifici esistenti relativi a . E '& # x27; s scioccante, e penso che l'uomo si sia reso assolutamente inadatto a correre per l'ufficio , & quot; 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_n_6820704.html? Utm_hp_ref = california-siccità & quot; & gt; causato siccità nel suo stato & lt;/a & gt ;, così come freddo intenso e tempeste sulla costa orientale. & lt;/p & gt; & lt; p & gt; Mentre Cruz potrebbe aver visto la neve e il ghiaccio ovunque nel New Hampshire, i dati risulti che il paese è in realtà vivendo un & lt; a href = & quot; http://www.huffingtonpost.com/2015/02/19/cold-weather- inverno_n_6713104.html & quot; & gt; più caldo di medio & lt;/a & gt; inverno. & lt;/p & gt; & lt; p & gt; le critiche di Brown di Cruz arriva un giorno prima della Texas senatore è impostato per annunciare una & lt; a href = & quot; http://www.huffingtonpost.com/2015/03/22 /ted-cruz-2016_n_6917824.html & quot; & gt; presidenziale campagna & lt;/a & gt ;.& lt;/p & gt; & lt;/div & gt; & lt; div classe = & quot; originale & quot; & gt; & lt; a href = & quot; http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html">VIEW 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 & 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; (funzione (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 * nuovo Date(); a = s.createElement (o), m = s.getElementsByTagName (o) [0]; a.async = 1; a.src = g; m.parentNode.insertBefore (a, m) }) (finestra, documento, & # x27; lo script & # x27 ;, & # x27; // www.google-analytics.com/analytics.js & # x27 ;, & # x27; ga & # x27;); ga (& # x27; creare & # x27 ;, & # x27; UA-56.257.533-1 & # x27 ;, & # x27; auto & # x27;); ga (& # x27; invia & # x27 ;, & # x27; visualizzazione pagina & # x27;); & lt;/script & gt; & lt;/body & gt; & lt;/html & gt;"

risposta

5

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 = "<!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

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