views:

61

answers:

1

Hi all. I'm having troubles when I want show a JSON response. I extract correctly the strings but when there is characters like 'á' or '¿' instead of these ones appears 'Ãi' or '¿'. I think that the response comes in non-UTF8 enconding, but I cannot find how to convert it.


EDIT: I'm trying to do a GET request to this URL: https://graph.facebook.com/me to retrieve the info of my profile passing the access token.

HttpGet get = new HttpGet(url);
response = client.execute(get);

To extract the response I do this:

JSONObject json_object;
response.getEntity().writeTo(ostream);
json_object = new JSONObject(ostream.toString());
String name = json_object.getString("name");
Log.d("NAME",name);

Note that I'm programming with Android SDK.


Thanks.

+1  A: 

You can do it by using import org.apache.commons.lang.StringEscapeUtils,if you don't have please download it.

for example

You can use it by StringEscapeUtils strutil = new StringEscapeUtils(); and then title= strutil.escapeHtml(title);

title    = title.replaceAll("\"", "''");
        desc_val = desc_val.replaceAll("\"","''");

        title = strutil.escapeHtml(title);
        desc_val = strutil.escapeHtml(desc_val);
        url_val = strutil.escapeHtml(url_val);

and also try to convert it into HTMl form,by using Html.fromHtml(title);

I hope it may help you.

MGS
I've tried that, but when I receive this String: "¿Tienes hambre?", I get the same string if I hadn't used the escapeHtml method: "¿Tienes hambre?". The Html.fromHtml method I think it's useless for me because I only want to get the correct encoding for the strings.
Adrian
please refer the answer again i edited few things there
MGS
whether if your content have double quote..
MGS
Ok, doing that converts the strange characters in HTML code, but I've read that the Spanned class (escapeHtml returns an object of that class) it's for represent styled text in a textview, but I want to store the string with the correct encoding. Is that possible? If not, I suppose that I can store that a Spannable object. Thanks
Adrian
i can't get you,what you asking about?let me clear little bit.
MGS
I want to store the string with the correct enconding, I means, if the response is ¿Tienes hambre? I want to store in a String object ¿Tienes hambre?. If I use escapeHtml method I get ¿Tienes hambre?. The fromHtml method returns a Spannable object, but I want to store the strings in a String object and the toString method of Spannable object is not suitable. As I said, if is not possible to convert it to a String object, I'll store it as a Spannable object.
Adrian
Well, I found that after doing the escapeHtml to the String and text=Html.fromHtml, when I show the String in a TextView doing this: "holder.text.setText(text,BufferType.SPANNABLE);" appears correctly. Thank you.
Adrian