I have tried implementing cross platform serialization between Java and Android. I have used Serializable, and having my code in Android in the same package as in desktop Java.
Source: java-desktop serializing
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
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. On desktop I serialize instance of student to "thestudent.dat". I put this file on SD card at Android device and I am trying to deserialize it. I am getting error 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.
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.
A quick way to debug this is to invoke getClass() on received Student object and getName() on Student class on receiver. 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. So if it is "com.acme.Student" on desktop, it should also be that on Android.
Also its good idea to add serialVersionUID
in case your Student
changes in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With