IT TIP

Android에서 HTML 엔티티 디코딩

itqueen 2020. 11. 26. 20:35
반응형

Android에서 HTML 엔티티 디코딩


HTML 엔티티를 디코딩해야합니다 (예 : & # 246; ö 및 & amp; &.

URLEncoder.decode(str)작업을 수행하지 않습니다 (% 표기법에서 변환). TextUtils에는 HTMLencode가 있지만 HTMLdecode는 없습니다.

HTML 엔터티를 디코딩하는 기능이 있습니까?


HTML을 클래스는 그러나이 모든 것이 지원되지 않는다고, 그렇게 할 예정이다. 그것은 항상 나를 위해 일했지만 나는 결코 ö를 가지지 않았으므로 이것을 말할 수 없습니다. 시도 Html.fromHtml(yourStr)디코딩 된 문자열을 얻을 수 있습니다.


Html.fromHtml (String html)은 API v24 이후 더 이상 사용되지 않으므로 올바른 방법입니다.

  if (Build.VERSION.SDK_INT >= 24)
  {
       textView.setText(Html.fromHtml(htmlString , Html.FROM_HTML_MODE_LEGACY)));  
  }
  else
  {
       textView.setText(Html.fromHtml(htmlString));
  }

이 코드를 사용하여 간단히 수행 할 수 있습니다.

  Html.fromHtml(String).toString();

이것이 당신을 도울 수 있기를 바랍니다


아래 단계에 따라 WebView를 사용하여 HTML 텍스트를 쉽게 나타낼 수 있습니다.

먼저 데이터를 html 형식으로 다음과 같이 변환하십시오.

String res=null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
res=Html.fromHtml(product.getDescription(),Html.FROM_HTML_MODE_COMPACT).toString();
}
else{
res=Html.fromHtml(product.getDescription()).toString();
}

그런 다음 WebView에서 데이터를 다음과 같이로드합니다.

myWebView.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);

다음을 호출하여 문자열에서 특수 문자를 제거 할 수 있습니다.

responsestring.replace(“special char here”, “”);

응답을 htmlstring에서 문자열로 변환 할 수 있습니다.-Html.fromHtml (response string here)하지만이 메서드는 API 24에서 감가 상각되므로 올바른 방법으로 수행해야합니다.

if (Build.VERSION.SDK_INT >= 24)
{
    post_description.setText(Html.fromHtml( response here , Html.FROM_HTML_MODE_LEGACY));
}
else
{
    post_description.setText(Html.fromHtml( response here ));
}

참고 URL : https://stackoverflow.com/questions/2918920/decode-html-entities-in-android

반응형