简体   繁体   English

Java序列化 - Android反序列化

[英]Java serialization - Android deserialization

I have tried implementing cross platform serialization between Java and Android. 我尝试在Java和Android之间实现跨平台序列化。 I have used Serializable, and having my code in Android in the same package as in desktop Java. 我使用了Serializable,并将Android中的代码放在与桌面Java相同的包中。

Source: java-desktop serializing 来源:java-desktop序列化

    Student student=new Student(); 
    student.setName("John");
    student.setSurname("Brown");
    student.setNumber(776012345);

    try {
      FileOutputStream fout = new FileOutputStream("thestudent.dat");
      ObjectOutputStream oos = new ObjectOutputStream(fout);
      oos.writeObject(student);
      oos.close();
    }
      catch (Exception e) { e.printStackTrace(); }

    }

Source: Android - deserializing 来源:Android - 反序列化

File file=new File(getExternalFilesDir(null), "thestudent.dat");

    try {

      FileInputStream fint = new FileInputStream(file);
      ObjectInputStream ois = new ObjectInputStream(fint);
      Student stud=(Student) ois.readObject();
      ois.close();
    }
      catch (Exception e) { e.printStackTrace(); }

}

Student is a class, which implements Serializable. Student是一个实现Serializable的类。 On desktop I serialize instance of student to "thestudent.dat". 在桌面上我将学生的实例序列化为“thestudent.dat”。 I put this file on SD card at Android device and I am trying to deserialize it. 我把这个文件放在Android设备的SD卡上,我试图反序列化它。 I am getting error java.lang.ClassCastException: javaserializace.Student . 我收到错误java.lang.ClassCastException:javaserializace.Student But why? 但为什么? I have same package when serializing, same package when deserializing. 序列化时我有相同的包,反序列化时包含相同的包。 All what is different is project name. 所有不同的是项目名称。 Do you see any solution? 你看到任何解决方案吗?

Edited - source of Student class: 编辑 - 学生班的来源:

public class Student implements Serializable {

private String name;
private String surname;
private int number;
private char gender;
private int age;
private long rc;
private int id;

public Student(){
    ;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public char getGender() {
    return gender;
}

public void setGender(char gender) {
    this.gender = gender;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public long getRc() {
    return rc;
}

public void setRc(long rc) {
    this.rc = rc;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

}

I am certain that two versions of Student on Both sides are not same. 我确信双方的两个版本的学生不一样。

Because the exception is 因为例外是

java.lang.ClassCastException: javaserializace.Student. java.lang.ClassCastException:javaserializace.Student。

which indicates that Java has successfully deserialized the object but while typecasting it to Student on receiver side, it is throwing exception because types are not same. 这表明Java已经成功地反序列化了该对象,但是当它在接收器端将其类型转换为Student时,它会抛出异常,因为类型不同。

A quick way to debug this is to invoke getClass() on received Student object and getName() on Student class on receiver. 调试它的一种快速方法是在接收到的Student对象上调用getClass(),在接收器上调用Student类上的getName() I am sure that in this case both are different. 我相信在这种情况下两者都不同。

And solution will be to ensure that Student on receiver side is of same type. 解决方案是确保接收方的学生属于同一类型。

I don't see any problem with the code you post. 我发布的代码没有任何问题。 My guess is that Student is not in the same exact package on both sides. 我的猜测是, Student双方并不是完全一致的。 So if it is "com.acme.Student" on desktop, it should also be that on Android. 因此,如果它是桌面上的“com.acme.Student”,它也应该是Android上的。

Also its good idea to add serialVersionUID in case your Student changes in the future. 另外,最好添加serialVersionUID以防Student将来更改。

I would recommend using something like Kryo or Protocol Buffers for your serialization. 我建议使用像KryoProtocol Buffers这样的序列化。 It would be much faster with smaller payload. 较小的有效载荷会快得多。 The only downside is that you need to include an additional jar. 唯一的缺点是你需要包括一个额外的罐子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM