HTML 문자열을 표시 할 수 없습니다.
Android WebView에서 표시 문자열 HTML로 어려움을 겪고 있습니다. 서버 측에서는 웹 페이지를 다운로드하고 HTML 문자와 따옴표를 이스케이프 처리했습니다 (Python 사용).
my_string = html.escape(my_string, True)
Android 클라이언트 측에서 : 문자열은 다음과 같이 이스케이프되지 않습니다.
myString = StringEscapeUtils.unescapeHtml4(myString)
webview.loadData( myString, "text/html", "encoding");
그러나 webview는 리터럴 문자열로 표시합니다. 결과는 다음과 같습니다.
편집 : 서버 측에서 반환 된 원래 문자열을 추가합니다.
link rel = "apple-touch-icon & quot; 크기 = & quot; 114x114 & quot; href = & quot; /static/favicon/apple-touch-icon-114x114.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; 크기 = & quot; 72x72 & quot; href = & quot; /static/favicon/apple-touch-icon-72x72.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; 크기 = "144x144" href = & quot; /static/favicon/apple-touch-icon-144x144.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; 크기 = "60x60" href = & quot; /static/favicon/apple-touch-icon-60x60.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; 크기 = & quot; 120x120 & quot; href = & quot; /static/favicon/apple-touch-icon-120x120.png" & gt; & lt; 링크 rel = & quot; 애플 터치 아이콘 & quot; 크기 = & quot; 76x76 & quot; href = & quot; /static/favicon/apple-touch-icon-76x76.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; 크기 = "152x152" href = & quot; /static/favicon/apple-touch-icon-152x152.png" & gt; & lt; link rel = & quot; apple-touch-icon & quot; 크기 = & quot; 180x180 & quot; href = & quot; /static/favicon/apple-touch-icon-180x180.png" & gt; & lt; link rel = & quot; 아이콘 & quot; type = & quot; 이미지 / png & quot; href = & quot; /static/favicon/favicon-192x192.png" 크기 = "192x192"& gt; & lt; link rel = & quot; 아이콘 & quot; type = & quot; 이미지 / png & quot; href = & quot; /static/favicon/favicon-160x160.png" 크기 = "160x160"& gt; & lt; 링크 rel = & quot; 아이콘 & quot; type = & quot; 이미지 / png & quot; href = & quot; /static/favicon/favicon-96x96.png" 크기 = & quot; 96x96 & quot; & gt; & lt; link rel = & quot; 아이콘 & quot; type = & quot; 이미지 / png & quot; href = & quot; /static/favicon/favicon-16x16.png" 크기 = "16x16"& gt; & lt; link rel = & quot; 아이콘 & quot; type = & quot; 이미지 / png & quot; href = & quot; /static/favicon/favicon-32x32.png" 크기 = "32x32"& gt; & lt; 메타 이름 = & quot; msapplication-TileColor & quot; content = & quot; # da532c & quot; & gt; & lt; 메타 이름 = & quot; msapplication-TileImage & quot; content = "/ static / favicon / mstile-144x144.png"& gt; & lt; 메타 이름 = & quot; msapplication-config & quot; content = & quot; /static/favicon/browserconfig.xml" & gt; & lt;!-외부 CSS-& gt; & lt; link rel = & quot; 스타일 시트 & quot; href = & quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">; & lt;!-외부 글꼴-& gt; & lt; link href = & quot; // maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel = & quot; 스타일 시트 & quot; & gt; & lt; link href = & # x27; // fonts.googleapis.com/css?family=Open+Sans:300,600' rel = & # x27; 스타일 시트 & # x27; type = & # x27; text / css & # x27; & gt; & lt; link href = & # x27; // fonts.googleapis.com/css?family=Lora:400,700' rel = & # x27; 스타일 시트 & # x27; type = & # x27; text / css & # x27; & gt; & lt;!-[IE 9 인 경우] & gt; & lt; script src = & quot; // cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script> & lt; script src = "// cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script> & lt;! [endif]-& gt; & lt ;! -사이트 CSS-& gt; & lt; link rel = & quot; 스타일 시트 & quot; type = & quot; 텍스트 / css & quot; href = & quot; /static/css/style.css" & gt; & lt; link rel = & quot; 스타일 시트 & quot; type = & quot; 텍스트 / css & quot; href = & quot; /static/css/glyphicon.css" & gt; & lt; / 헤드 & gt; & lt; 본문 & gt; & lt; div class = & quot; 컨테이너 기사 페이지 & quot; & gt; & lt; div class = & quot; 행 & quot; & gt; & lt; div 클래스 = "col-md-8 col-md-offset-2"& gt; & lt; h2 & gt; & lt; a href = & quot; css & quot; & gt; & lt; / 헤드 & gt; & lt; 본문 & gt; & lt; div class = & quot; 컨테이너 기사 페이지 & quot; & gt; & lt; div class = & quot; 행 & quot; & gt; & lt; div 클래스 = "col-md-8 col-md-offset-2"& gt; & lt; h2 & gt; & lt; a href = & quot; css & quot; & gt; & lt; / 헤드 & gt; & lt; 본문 & gt; & lt; div class = & quot; 컨테이너 기사 페이지 & quot; & gt; & lt; div class = & quot; 행 & quot; & gt; & lt; div 클래스 = "col-md-8 col-md-offset-2"& gt; & lt; h2 & gt; & lt; a href = & quot;http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html"> 정부 . Jerry Brown은 Ted Cruz가 & amp; # 39; 절대적으로 부적합합니다 & amp; # 39; 기후 변화로 인한 사무실 출마 & lt; / a & gt; & lt; / h2 & gt; & lt; h4 & gt; 샘 레빈 & lt; / h4 & gt; & lt; div class = & quot; article & quot; & gt; & lt; p & gt; 캘리포니아 주지사 제리 브라운 (D)은 일요일에 텍사스 상원 의원 테드 크루즈 (R- 텍사스)가 "직접 출마하기에 절대적으로 부적합하다"고 말했다. 기후 변화에 대한 그의 입장 때문입니다. & lt; / p & gt; & lt; p & gt; & quot; 저는 사방에 눈과 얼음이있는 뉴햄프셔에서 방금 돌아 왔습니다. 이것에 대한 나의 견해는 간단합니다. 이에 대한 토론은 과학을 따라야하고 데이터를 따라야합니다. 과학이 그들을 뒷받침하지 않기 때문에 문제가 있습니다. 크루즈 & lt; a href = & quot;https://www.youtube.com/watch?v=m0UJ_Sc0Udk">said</a> ; & quot; Seth Meyers의 심야 & quot; 지난주. & lt; / p & gt; & lt; p & gt; 크루스는 자신의 주장을 뒷받침하기 위해 지난 17 년 동안 상당한 온난화가 부족한 위성 데이터를 인용했습니다. 하지만 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; 과학자들은 기후가 계속해서 따뜻해질 것이라고 믿을 수있는 충분한 증거를 가지고 있음을 보여주는 Politifact & lt; / a & gt;에 의해 반박되었습니다. & lt; / p & gt; & lt; p & gt; & quot; 그가 말한 것은 절대적으로 거짓입니다. "Brown은 & lt; a href = & quot; http : //www.nbcnews.unfit-be-running-n328046 "& gt; NBC & # x27; s"Meet the Press. "& lt; / a & gt; 그는 & lt; a href = & quot; http://climate.nasa.gov/scientific-consensus/">90% 이상 & lt; / a & gt; 기후를 연구하는 과학자의 50 %는 기후 변화가 인간 활동에 의해 발생한다는 데 동의합니다. '그 사람은 그러한 수준의 무지와 기존 과학 데이터의 직접적인 위조를 낳았습니다. 충격적입니다. 그리고 저는 남자가 자신을 사무실에 출마하기에 절대적으로 부적합하게 만들었다 고 생각합니다. & quot; 브라운이 말했다. & lt; / p & gt; & lt; p & gt; Brown은 기후 변화에 & 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; 그의 주에서 가뭄을 일으켰고 & lt; / a & gt; 동해안에서 심한 추위와 폭풍을 일으켰습니다. & lt; / p & gt; & lt; p & gt; 크루스는 뉴햄프셔의 모든 곳에서 눈과 얼음을 보았을 지 모르지만, 데이터에 따르면이 나라는 실제로 & lt; a href = & quot; http://www.huffingtonpost.com/2015/02/19/cold-weather- winter_n_6713104.html & quot; & gt; 평균보다 따뜻함 & lt; / a & gt; 겨울. & lt; / p & gt; & lt; p & gt; 브라운의 크루즈에 대한 비판은 텍사스 상원 의원이 & lt; a href = & quot; http://www.huffingtonpost.com/2015/03/22 /ted-cruz-2016_n_6917824.html"> 대통령 캠페인 & lt; / a & gt ;. & lt; / p & gt; & lt; / div & gt; & lt; div class = & quot; 원본 & quot; & gt; & lt; a href = & quot;http://www.huffingtonpost.com/2015/03/22/ted-cruz-climate-change_n_6919002.html">VIEW원본 & 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; 스크립트 src = & quot; /static/js/modal.js" & gt; & lt; / script & gt; & lt; script src = & quot; /static/js/bootbox.min.js> & lt; / script & gt; & lt; 스크립트 src = & quot; /static/js/site.js" & gt; & lt; / script & gt; & lt; 스크립트 & 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; 만들기 & # x27 ;, & # x27; UA-56257533-1 & # x27 ;, & # x27; 자동 & # x27;); ga (& # x27; 보내기 & # x27 ;, & # x27; 페이지 뷰 & # x27;); & lt; / 스크립트 & gt; & lt; / 본문 & gt; & lt; / html & gt; "
여기에서 코드를 수정했습니다.
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 &#39;Absolutely Unfit&#39; 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();
}
}

이 코드를 시도하십시오.
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));
}
이 시도:
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();
}
참고 URL : https://stackoverflow.com/questions/29208984/cannot-display-html-string
'Program Club' 카테고리의 다른 글
| matplotlib : AxesSubplot 객체를 생성 한 다음 Figure 인스턴스에 추가 할 수 있습니까? (0) | 2020.12.05 |
|---|---|
| 콘텐츠 내에서 정확한 드래그 앤 드롭 (0) | 2020.12.05 |
| Facebook에서 Chrome에서 "Unsafe JavaScript attempt to access frame with URL"오류를 표시합니다. (0) | 2020.12.05 |
| MySql에서 악센트 구분 검색을 수행하는 방법 (0) | 2020.12.04 |
| Python에서 문자열 변수 초기화 : ""또는 None? (0) | 2020.12.04 |