Vikas
Vikas

Reputation: 24332

Android: HTML encoder

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?

Upvotes: 0

Views: 5370

Answers (3)

Senthil Mg
Senthil Mg

Reputation: 3313

try to use either Html.fromhtml(string) or StringEscapeutil utility(download this jar) after downloading it,lets try something like below

StringEscapeUtils strutil = new StringEscapeUtils(); title = strutil.escapeHtml(title);

...I think it may help you..

Upvotes: 0

twig
twig

Reputation: 4184

I know this is a few months old, but TextUtils.htmlEncode() is a great little method that does exactly just that.

Upvotes: 10

Vikas
Vikas

Reputation: 24332

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.

Upvotes: 0

Related Questions