Java serialization - Android deserialization

I tried to implement cross-platform serialization between Java and Android. I used Serializable and have my Android code in the same package as Java desktop.

Source: java-desktop serialization

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 - deserialization

 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(); } } 

A student is a class that implements Serializable. On the desktop, I serialize the student instance to "thestudent.dat". I put this file on an SD card on an Android device and I am trying to deserialize it. I get the error java.lang.ClassCastException: javaserializace.Student . But why? I have the same package when serializing, the same package when deserializing. All that distinguishes the name of the project. Do you see any solution?

Edited - Student class source:

 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; } } 
+6
source share
3 answers

I am sure that the two versions of Student on both sides are not the same.

Because the exception

java.lang.ClassCastException: javaserializace.Student.

which indicates that Java successfully deserialized the object, but when typing it on Student on the receiver side, it throws an exception because the types do not match.

A quick way to debug is to call getClass () on the accepted Student object and getName () on the Student class on the receiver. I am sure that in this case both options are different.

And the solution will be to make sure that Student on the receiver side is of the same type.

+4
source

I see no problems with the code you publish. I assume Student not in the same package on both sides. So if it is "com.acme.Student" on the desktop, it should also be on Android.

It's also a good idea to add serialVersionUID in case your Student changes in the future.

+1
source

I would recommend using something like Kryo or Protocol Buffers for your serialization. That would be much faster with less payload. The only drawback is that you need to include an additional jar.

0
source

Source: https://habr.com/ru/post/897889/


All Articles