在android中解码HTML实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2918920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decode HTML entities in android
提问by johboh
I need to decode HTML entities, e.g. from ö to ö, and & to &.
我需要解码 HTML 实体,例如来自 ö 到 ö 和 & 到 &。
URLEncoder.decode(str)
does not do the job (convert from % notations). TextUtils has a HTMLencode, but not a HTMLdecode.
URLEncoder.decode(str)
不做这项工作(从 % 符号转换)。TextUtils 有一个 HTMLencode,但没有一个 HTMLdecode。
Are there any function for decoding HTML entities?
有没有解码 HTML 实体的函数?
回答by Sephy
The Html classis supposed to do that, however it is said that everything is not supported. It always worked for me but I never had ? so I can't tell for this one.
Try Html.fromHtml(yourStr)
to get the decoded string.
该HTML的class是应该做的,但它是说,一切都没有支持。它总是对我有用,但我从来没有?所以我不能说这个。尝试Html.fromHtml(yourStr)
获取解码后的字符串。
回答by Aalok
Html.fromHtml(String html) is deprecatedafter API v24 so this is the correct way to do it
在 API v24之后不推荐使用Html.fromHtml(String html)所以这是正确的方法
if (Build.VERSION.SDK_INT >= 24)
{
textView.setText(Html.fromHtml(htmlString , Html.FROM_HTML_MODE_LEGACY)));
}
else
{
textView.setText(Html.fromHtml(htmlString));
}
回答by saeed
Simply you can do that using this code
只需使用此代码即可做到这一点
Html.fromHtml(String).toString();
Hope this will help you
希望能帮到你
回答by Ankit Dubey
you can use WebView to represent any html text easily by following below steps.
您可以按照以下步骤使用 WebView 轻松表示任何 html 文本。
first convert your data in html format as :
首先将您的数据以 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();
}
Then load your data in WebView as:
然后将您的数据加载到 WebView 中:
myWebView.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);
回答by Kamal Bunkar
You can remove special char from string by calling
您可以通过调用从字符串中删除特殊字符
responsestring.replace(“special char here”, “”);
you can convert response into a string from htmlstring like that – Html.fromHtml( response string here ) But this method is depreciated on API 24 so you need to do it in a correct way-
您可以像这样将响应从 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 ));
}