tags:

views:

69

answers:

2

I am downloading a JSONObject from a web site. The entries are however HTML-encoded, using

" 

and

&

tags. Is there an easy way to get these to Java strings? Short of writing the converter myself, of course.

Thanks RG

PS: I am using the stuff in a ListView. Probably I can use Html.fromHTML as I can for TextView. Don't know.

+1  A: 

I've heard of success in using the Apache Commons on Android.

You should be able to use StringEscapeUtils.unescapeHtml() (from the Lang package).

Here are the (fairly straightforward) directions on using the Apache Commons libraries in your Android apps: Importing org.apache.commons into android applications.

Justin Niessner
A: 

OK, I simply went to write my own quick fix. Not efficient, but that's OK for the purpose. A 5-minutes-solution.

public static String unescape (String s)
{
    while (true)
    {
        int n=s.indexOf("&#");
        if (n<0) break;
        int m=s.indexOf(";",n+2);
        if (m<0) break;
        try
        {
            s=s.substring(0,n)+(char)(Integer.parseInt(s.substring(n+2,m)))+
                s.substring(m+1);
        }
        catch (Exception e)
        {
            return s;
        }
    }
    s=s.replace("&quot;","\"");
    s=s.replace("&lt;","<");
    s=s.replace("&gt;",">");
    s=s.replace("&amp;","&");
    return s;
}
Rene