我有一个记录应用程序代码,当我尝试阅读保存的笔记时,它给出了我这样的错误:
res.sendStatus(204);
这是代码:
Utilities.java
java.io.InvalidClassException: com.slide.adarsh.ezswipe.Note; Incompatible class (SUID): com.slide.adarsh.ezswipe.Note: static final long serialVersionUID =3563451862165282381L; but expected com.slide.adarsh.ezswipe.Note: static final long serialVersionUID =0L;
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2341)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at com.slide.adarsh.ezswipe.Utilities.getAllSavedNotes(Utilities.java:62)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at com.slide.adarsh.ezswipe.NoteList.onResume(NoteList.java:32)
这是我的注意类,它实现了public class Utilities {
public static final String FILE_EXTENSION=".bin";
public static boolean saveNote(Context context,Note note) {
String fileName=String.valueOf(note.getDateTime())+FILE_EXTENSION;
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos=context.openFileOutput(fileName,context.MODE_PRIVATE);
oos=new ObjectOutputStream(fos);
oos.writeObject(note);
oos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static ArrayList<Note> getAllSavedNotes(Context context){
ArrayList<Note> not=new ArrayList<Note>();
File filesDir=context.getFilesDir();
ArrayList<String> noteFiles=new ArrayList<String>();
for (String file : filesDir.list()){
if (file.endsWith(FILE_EXTENSION)){
noteFiles.add(file);
}
}
FileInputStream fis;
ObjectInputStream ois;
for (int i=0;i<noteFiles.size();i++) {
try {
fis=context.openFileInput(noteFiles.get(i));
ois=new ObjectInputStream(fis);
not.add((Note) ois.readObject()); //this is the place where exception is raised
fis.close();
ois.close();
} catch (IOException|ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
return not;
}
}
:
Serializable
答案 0 :(得分:0)
这是因为你没有为你的Note pojo添加serialVersionUID
。
实施Serializable
的每个班级都需要添加serialVersionUID
您可以将注释更改为:
public class Note implements Serializable {
private static final long serialVersionUID = 1234567L;
...
...
}
但 不应该 依赖Serializable来保持数据持久性,因为每次数据更改时,可能会与以前的数据发生一些不兼容的更改。在我看来,可序列化对象应该用于临时使用。
答案 1 :(得分:0)
每个序列化对象应该具有串行版本uuid。如果你不提供它,它将自动随机。
每次你建立新的apk时,它都有机会改变你的包。
由于已保存的对象已经存在,并且您尝试使用不同的uuid恢复它们,这是失败的原因。
解决方案1:忘记旧数据并使用提供的uuid创建新文件。我知道这听起来很奇怪。
幸运的是,如果你有以前的apk,你仍然可以恢复它们。在线检查反编译apk并找到想要恢复的类,并获得随机uuid。这是你的恢复密钥。把它添加到你的班级。
祝你好运