-4

Whenever I parse JSON string and display the output of json output in android Logcat, I always found

"f������a������m������i������l������y������.������p������n������g"

in Logcat. The actual string is "family.png" but it displays as above. Any idea how to solve this issue?

Here is JSON data.

{"members": [
   {"user":"���d���e���8���8���f���5���c���7���3���7���1���4���7���6���6���f", "username":"���P���P���S���h���e���i���n", "avatar":"f������a������m������i������l������y������.������p������n������g"} 
    ]}

and generated JSON format is by Coldfusion.

{"members": [
<cfoutput query="getQry">
{"user":"#tuser#", "username":"#tusername#", "avatar":"#tpicture#"} <cfif currentrow LT getTrackQry.recordcount>,</cfif>
</cfoutput>
]}

Here is parse JSON object from URL.

public static JSONObject getFromUrl(String url) {
        InputStream is = null;
        String content = null;
        JSONObject jArray = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(url));
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        catch(Exception e){
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        try{
              BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf8"),8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              content = sb.toString();
        } catch(Exception e){
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {
            jArray = new JSONObject(content);
        } catch (JSONException e){
            Log.e("log_tag", "Error parsing data " + e.toString());
        }           

        return jArray;      
    }
PPShein
  • 13,309
  • 42
  • 142
  • 227
  • Do you control the JSON data? Do you generate it yourself? Show use the code. Show us the JSON. – Jared Burrows Apr 29 '15 at 03:17
  • yap, I've generated JSON data by myself and added JSON data in my question. – PPShein Apr 29 '15 at 03:20
  • @Pang I know. That's why I'm asking why "family.png" get wrong to "f������a������m������i������l������y������.������p������n������g" in android logcat. – PPShein Apr 29 '15 at 03:26
  • 1
    @ppshein Ok we are close. Post the code you used to parse it. – Jared Burrows Apr 29 '15 at 03:27
  • How are you supposed to get `family.png` if you JSON data does not contain it (your JSON data contains `female.png` though)? – Pang Apr 29 '15 at 03:27
  • @JaredBurrows I've added about coding to parse JSON object from URL. – PPShein Apr 29 '15 at 03:30
  • I suggest writing `content` to LogCat to see if the string is corrupted before or after converted to JSON. – Pang Apr 29 '15 at 03:43
  • @Pang as usual displaying "content" in Logcat. – PPShein Apr 29 '15 at 03:53
  • So you're seeing `family.png` when you write the value of the variable `content` to LogCat? – Pang Apr 29 '15 at 03:55
  • see this question http://stackoverflow.com/questions/6224899/serializejson-doesnt-encode-utf8-characters-in-coldfusion-9 – Hector Apr 29 '15 at 04:01
  • @Pang nope.. as usual. – PPShein Apr 29 '15 at 04:07
  • 2
    `new BufferedReader(new InputStreamReader(is,"utf8"),8)` should be `new BufferedReader(new InputStreamReader(is, "UTF-8"))`, see http://stackoverflow.com/questions/6511880/how-to-parse-a-json-input-stream. – Jared Burrows Apr 29 '15 at 04:22
  • regarding @Hector comment, my json object is just like in my question. Please check question, sorry about that. – PPShein Apr 29 '15 at 04:22
  • @ppshein Can you choose an answer? – Jared Burrows Apr 29 '15 at 04:48

2 Answers2

1

It seems like you are using the incorrect charset. Have you tried changing this line from:

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf8"),8);

to

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
Christian Abella
  • 5,747
  • 2
  • 30
  • 42
  • 1
    I happened to be viewing my code here and noticed that the charset used is the culprit. was not looking at the comments. too many. – Christian Abella Apr 29 '15 at 04:36
1

Change:

new BufferedReader(new InputStreamReader(is,"utf8"),8)

To:

new BufferedReader(new InputStreamReader(is, "UTF-8"))

References:

Please See the other SO question: How to parse a JSON Input stream.

Official docs for public InputStreamReader(InputStream in, CharsetDecoder dec) are here.

UTF-8 charset information is also located here.

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185