0

I'm getting this error while trying to create a JSONObject in android

org.json.JSONException: Value {"0" : [{"original" : "car", "translation" : "araba"}]} of type java.lang.String cannot be converted to JSONObject

I get the above error if I use the string returned from the server using HttpClient.

If I take the same string above and define a static string and parse that it's fine. Is there some decoding or encoding issue that I have to account for when using HttpClient to retrieve the JSON?

I'm building the JSON String manually in c# as a test and returning it

return "{\"0\" : [{\"original\" : \"car\", \"translation\" : \"araba\"}]}";

here is the code

    HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        String s = "http://10.0.2.2:63200/OnlineArabic/api/datasen";
        HttpGet httpGet = new HttpGet(s);

    HttpResponse response = httpClient.execute(httpGet, localContext);
    HttpEntity entity = response.getEntity();
    InputStream instream = entity.getContent();

     BufferedReader reader = new BufferedReader(new InputStreamReaderinstream , "UTF-8"),8 );
         StringBuilder sb = new StringBuilder();

         String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }


        String result= convertStreamToString(instream);             
    String jsonString = sb.toString();
    JSONObject jObject = new JSONObject(jsonString);

Any idea?

Here is the log:

12-01 13:48:08.156: W/System.err(22947): org.json.JSONException: Value {"0" : [{"original" : "car", "translation" : "araba"}]} of type java.lang.String cannot be converted to JSONObject
12-01 13:48:08.160: W/System.err(22947):    at org.json.JSON.typeMismatch(JSON.java:107)
12-01 13:48:08.160: W/System.err(22947):    at org.json.JSONObject.<init>(JSONObject.java:158)
12-01 13:48:08.160: W/System.err(22947):    at org.json.JSONObject.<init>(JSONObject.java:171)
12-01 13:48:08.170: W/System.err(22947):    at com.ls.audioplayer.MainActivity.call(MainActivity.java:137)
12-01 13:48:08.170: W/System.err(22947):    at com.ls.audioplayer.MainActivity$1.onClick(MainActivity.java:294)
12-01 13:48:08.180: W/System.err(22947):    at android.view.View.performClick(View.java:2408)
12-01 13:48:08.180: W/System.err(22947):    at android.view.View$PerformClick.run(View.java:8816)
12-01 13:48:08.190: W/System.err(22947):    at android.os.Handler.handleCallback(Handler.java:587)
12-01 13:48:08.190: W/System.err(22947):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 13:48:08.221: W/System.err(22947):    at android.os.Looper.loop(Looper.java:123)
12-01 13:48:08.221: W/System.err(22947):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-01 13:48:08.221: W/System.err(22947):    at java.lang.reflect.Method.invokeNative(Native Method)
12-01 13:48:08.231: W/System.err(22947):    at java.lang.reflect.Method.invoke(Method.java:521)
12-01 13:48:08.231: W/System.err(22947):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 13:48:08.240: W/System.err(22947):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 13:48:08.240: W/System.err(22947):    at dalvik.system.NativeStart.main(Native Method)
3
  • 1. Logs please 2. Try a online json formatter/convertor to check your json Commented Dec 1, 2012 at 18:00
  • Where is your Android code for creating the JSONObject? Commented Dec 1, 2012 at 19:05
  • I've added the code that creates the JSONObject Commented Dec 1, 2012 at 23:44

3 Answers 3

2

Ensure that the server is sending UTF-8 encoded data. Also see that there are no new line characters in the beginning/end.

Eclipse's logcat tool will help you find these special characters. Logcat on command line sometimes doesnt render the special characters.

Sign up to request clarification or add additional context in comments.

1 Comment

The server had extra data so I cleared the stream and sent back only the json
0

My only though is that you are somehow destroying the string as it comes in with your httpget code, I would recommend you use a library for http requests such as EasyHttpClient , just create that class in your project and then instead of all that code you have do

EasyHttpClient client = new EasyHttpClient();
String jsonString = client.get("http://10.0.2.2:63200/OnlineArabic/api/datasen");

Then see if you can put that in an object. I cant test that code unfortunately I only have android 4.2 installed here.

Comments

0

In your code,

BufferedReader reader = new BufferedReader(new InputStreamReaderinstream , "UTF-8"),8 );

change this to

BufferedReader reader = new BufferedReader(new InputStreamReader(instream , "UTF-8"),8 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.