Ricard Molins
Ricard Molins

Reputation: 450

HTML encoding escaping problems

I'm doing an Android app in which I make a query to a webservice, get a JsonObject and after getting the desired String I find strings like: está

I've tried this two:

StringEscapeUtils.escapeHTML4(text);

With the result of transforming &aacute into &aacute

Html.escapeHtml(test));

Which does nothing.

Any ideas how too transform this into á or the corresponding character?

Upvotes: 1

Views: 8978

Answers (2)

Jorgesys
Jorgesys

Reputation: 126445

Will be enough with:

String myInitialtext = "est&á";  
Spanned mysString = Html.fromHtml(myInitialtext);
String result = mysString.toString();

now result has the value of est&á

Here is a similar question:

Android string encoding and html entities converting

Upvotes: 0

Nate
Nate

Reputation: 322

You stated you had used the following:

StringEscapeUtils.escapeHTML4(text);

Instead try this:

StringEscapeUtils.unescapeHTML4(text);

You were re-encoding the HTML entitites;

Documentation here:

https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringEscapeUtils.html

// import commons http://commons.apache.org
import org.apache.commons.lang3.StringEscapeUtils;

public static String stripHtml(String str) {
 return StringEscapeUtils.unescapeHtml4(str.replaceAll("<[A-Za-z/].*?>", "")).trim();
}

In addition, you can use this to decode other encoded types (JSON, XML, etc) or use it to encode.


This isn't what you asked but may also be useful for URL decoding:

String result = URLDecoder.decode(url, "UTF-8");

API reference here:

http://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html

Upvotes: 5

Related Questions