JSON Parsing works on Android 4.0 but not on Android < 4.0

android, json, parsing

Solution

It is possible that the `JSONObject` parser has been made more lenient in newer Android releases. The error message you are getting appears to be due to dubiously-legal JSON, particularly on the receiving side:

- Issue initializing a JSONObject

- JSON parsing problem

I would suggest that you write your downloaded JSON out to a file and compare it with your original to see if there is a problem with the download logic.

UPDATE

I cannot reproduce your problem. Loading that JSON off of external storage works perfectly fine on Android 4.0.3., 2.3.3, 2.2, and 2.1, using the following activity (note: I was lazy and hard-wired in the path to external storage):

package com.commonsware.jsontest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONTestActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
      BufferedReader in=
          new BufferedReader(new FileReader("/mnt/sdcard/test.json"));
      String str;
      StringBuilder buf=new StringBuilder();

      while ((str=in.readLine()) != null) {
        buf.append(str);
        buf.append("\n");
      }

      in.close();
      JSONObject json=new JSONObject(buf.toString());

      ((TextView)findViewById(R.id.stuff)).setText(json.toString());
    }
    catch (IOException e) {
      Log.e(getClass().getSimpleName(), "Exception loading file", e);
    }
    catch (JSONException e) {
      Log.e(getClass().getSimpleName(), "Exception parsing file", e);
    }
  }
}

Problem

Possible Duplicate: JSON parsing problem I am parsing a JSON file (Which is valid). It works on Android 4.0 - 4.0.4 but not on older Android versions. This is a part of my Manifest: ``` <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" /> ``` And this is my parsing code: ``` public JSONObject getJSONFromUrl(String url) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } return jObj; } ``` And on the older devices I get the following error message (But as I said not on new Android devices): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject I have absolutely no idea why it does work on Android 4 but not on older devices. Find the Json from here

Original source

Related problems