开发者

Is there a htmlDecode?

I am downloading a JSONObject from a web site. The entries are however HTML-encoded,开发者_StackOverflow社区 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.


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜