我已成功设法将用户在我的应用程序中输入的数据保存到应用程序数据文件夹中的JSON文件中。
现在,我试图在应用程序被销毁后将数据(如果存在)加载回应用程序。
这是我用来将其保存为JSON的代码
这是我的JSON保存和加载类:
public class CriminalIntentJSONSerializer {
private Context mContext;
private String mFilename;
public CriminalIntentJSONSerializer(Context c, String f) {
this.mContext = c;
this.mFilename = f;
}
public void saveCrimes(ArrayList<Crime> crimes) throws JSONException, IOException {
// Build an array in JSON
JSONArray array = new JSONArray();
for (Crime c: crimes) {
array.put(c.toJSON());
}
//Write the file to disk
Writer writer = null;
try {
OutputStream out = mContext.openFileOutput(mFilename, Context.MODE_PRIVATE);
writer = new OutputStreamWriter(out);
writer.write(array.toString());
} finally {
if (writer != null) {
writer.close();
}
}
}
public ArrayList<Crime> loadCrimes() throws IOException, JSONException {
ArrayList<Crime> crimes = new ArrayList<Crime>();
BufferedReader reader = null;
try {
// open and read the file into a StringBuilder
InputStream in = mContext.openFileInput(mFilename);
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder jsonString = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
// line breaks are omitted and irrelevant
jsonString.append(line);
}
// parse the JSON using JSONTokener
JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
// build the array of crimes from JSONObjects
for (int i = 0; i < array.length(); i++) {
crimes.add(new Crime(array.getJSONObject(i)));
}
} catch (FileNotFoundException e) {
// we will ignore this one, since it happens when we start fresh
} finally {
if (reader != null)
reader.close();
}
return crimes;
}
}
loadCrimes
似乎有问题。我试过调试它,似乎它成功找到了json文件,我设法在调试器窗口中看到json内容:
它还将数据保存到JSONArray
数组中,但是当它将其添加到crimes
数组时,它不起作用。数组的大小保持为0,因此实际上没有数据加载。它也不会抛出任何错误。
这是我的Crime
课程:
public class Crime {
private static final String JSON_ID = "id";
private static final String JSON_TITLE = "title";
private static final String JSON_SOLVED = "solved";
private static final String JSON_DATE = "date";
private UUID mID;
private String mTitle;
private Date mDate;
private boolean mSolved;
public Crime() {
// Generate unique identifier
this.mID = UUID.randomUUID();
this.mDate = new Date();
}
public Crime(JSONObject json) throws JSONException {
mID = UUID.fromString(json.getString(JSON_ID));
if (json.has(JSON_TITLE)) {
mTitle = json.getString(JSON_TITLE);
}
mSolved = json.getBoolean(JSON_SOLVED);
mDate = new Date(json.getLong(JSON_DATE));
}
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public UUID getmID() {
return mID;
}
public Date getmDate() {
return mDate;
}
public void setmDate(Date mDate) {
this.mDate = mDate;
}
public boolean ismSolved() {
return mSolved;
}
public void setmSolved(boolean mSolved) {
this.mSolved = mSolved;
}
@Override
public String toString() {
return mTitle;
}
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put(JSON_ID, getmID().toString());
json.put(JSON_TITLE, getmTitle());
json.put(JSON_SOLVED, ismSolved());
json.put(JSON_DATE, getmDate());
return json;
}
}
我正在使用Genymotion Emulator在ICS 4.4.2 API 19上运行该应用程序。在Android Studio 0.8.6上编码。
有人能确定我到底做错了什么吗?
EDIT。重新启动Android Studio后,它现在显示错误。这是堆栈跟踪。我发现这是一个简单的数据类型问题。解决:
08-20 15:34:13.019 2835-2835/? E/CrimeLab﹕ Error loading crimes:
org.json.JSONException: Value Wed Aug 20 15:26:15 GMT+00:00 2014 at date of type java.lang.String cannot be converted to long
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getLong(JSONObject.java:482)
at com.apps.criminalintent.Crime.<init>(Crime.java:35)
at com.apps.criminalintent.CriminalIntentJSONSerializer.loadCrimes(CriminalIntentJSONSerializer.java:68)
at com.apps.criminalintent.CrimeLab.<init>(CrimeLab.java:28)
at com.apps.criminalintent.CrimeLab.get(CrimeLab.java:61)
at com.apps.criminalintent.CrimeListFragment.onCreate(CrimeListFragment.java:37)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我认为您的问题来自您的Crime
构造函数:
mDate = new Date(json.getLong(JSON_DATE));
日期无法像Long一样进行解析。我认为你有一个JSONException。
编辑:您可以通过以毫秒为单位节省时间来解决问题:
json.put(JSON_DATE, getmDate().getTime());