6

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

}
7
  • Can you also post Student class? Commented Sep 23, 2011 at 11:53
  • 2
    Are they in a package? And are you sure it is really he exact same class? Commented Sep 23, 2011 at 12:28
  • 2
    You should have a serialVersionUID if nothing else. Also, why not just use JSON/etc? Commented Sep 23, 2011 at 12:47
  • yes, it has the same class name... Commented Sep 23, 2011 at 12:48
  • stackoverflow.com/questions/2917847/… Commented Sep 23, 2011 at 13:00

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Right... theye weren't the same, but they should be. I have to completely rework this deserialization task. Thanks
1

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.

1 Comment

Thanks, they are in the same packages... that's why i have written here.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.