我正在写一个文件的JSON响应,当我从文本文件中检索它时,它在json对象之前有一个null(真的很奇怪)。
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)
然后在打印出字符串时显示此信息。
05-30 16:35:16.454: I/json data in ProfileActivity(21734): null{"id":1488,"email":"xxx@xxx.com","full_name":"Jon
以下是我保存和检索数据的方法。
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;
}
答案 0 :(得分:1)
我明白了!
我换了一行:
String myData = null;
为:
String myData =“”;
myData被附加到另一个字符串,但是如果你向字符串追加一个null,你会在数据的前面得到一个奇怪的不可移除的空值。