1

I am writing a JSON response to a file and when I retrieve it from the text file it has a null before the json object (really weird).

05-30 16:35:16.454: W/System.err(21734): org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject
05-30 16:35:16.454: W/System.err(21734):    at org.json.JSON.typeMismatch(JSON.java:111)
05-30 16:35:16.454: W/System.err(21734):    at org.json.JSONObject.<init>(JSONObject.java:158)
05-30 16:35:16.454: W/System.err(21734):    at org.json.JSONObject.<init>(JSONObject.java:171)

It then shows this when printing out the string.

05-30 16:35:16.454: I/json data in ProfileActivity(21734): null{"id":1488,"email":"xxx@xxx.com","full_name":"Jon

Here is how I am saving and retrieving my data.

public void saveToInternal(String data, String name, Context context){
      ContextWrapper contextWrapper = new ContextWrapper(context);
      File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
      myInternalFile = new File(directory , name);
      try {
            FileOutputStream fos = new FileOutputStream(myInternalFile);
            fos.write(data.getBytes());
            fos.close();
           } catch (IOException e) {
            e.printStackTrace();
           }
      Log.i("data saved", "data named " + name + "saved to directory.");
}

public String getInternalData(String filename, Context context){
      ContextWrapper contextWrapper = new ContextWrapper(context);
      File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
      myInternalFile = new File(directory , filename);
    String myData = null;
     try {
            FileInputStream fis = new FileInputStream(myInternalFile);
            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = 
             new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
             myData = myData + strLine;
            }
            in.close();
           } catch (IOException e) {
            e.printStackTrace();
           }

    return myData;

}
4

1 回答 1

1

I figured it out!

I changed the line:

String myData = null;

to:

String myData = "";

myData was being appended to another string, but if you append a null to a string you get a weird un-removable null value at the front of the data.

于 2013-05-30T16:32:06.553 回答