Android:HTML编码器(Android: HTML encoder)

我有html文本,我想在webview中显示。

作为规范 ,数据必须进行URI转义。 所以我尝试使用URLEncoder.encode()函数,但这对我没用,因为它用加号转换空格。

那么有可用的编码功能吗?

I have html text that I want to show in webview.

As specification, data must be URI-escaped. So I tried to use URLEncoder.encode() function, but that will not help me, as it converts blank space with plus sign.

So is there any encoding function available?

最满意答案

我知道这已经有几个月了,但是TextUtils.htmlEncode()是一个很好的小方法,它正是这样做的。

As @Seb said, I created my own function that worked for me:

public String encodeHTML(String s) { s = s.replaceAll("#", "%23"); s = s.replaceAll("%", "%25"); return s; }

Any improvement in this approach will be appreciated.

Android:HTML编码器(Android: HTML encoder)

我有html文本,我想在webview中显示。

作为规范 ,数据必须进行URI转义。 所以我尝试使用URLEncoder.encode()函数,但这对我没用,因为它用加号转换空格。

那么有可用的编码功能吗?

I have html text that I want to show in webview.

As specification, data must be URI-escaped. So I tried to use URLEncoder.encode() function, but that will not help me, as it converts blank space with plus sign.

So is there any encoding function available?

最满意答案

我知道这已经有几个月了,但是TextUtils.htmlEncode()是一个很好的小方法,它正是这样做的。

As @Seb said, I created my own function that worked for me:

public String encodeHTML(String s) { s = s.replaceAll("#", "%23"); s = s.replaceAll("%", "%25"); return s; }

Any improvement in this approach will be appreciated.