Java序列化 - 反序列化的Andr​​oid [英] Java serialization - Android deserialization

查看:205
本文介绍了Java序列化 - 反序列化的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试实现Java和Android的跨平台序列化。我已经使用了序列化,并有我的code在Android在同一个包在桌面Java。

来源:Java的桌面串行化

 学生学生=新的学生();
    student.setName(约翰福音);
    student.setSurname(布朗);
    student.setNumber(776012345);

    尝试 {
      FileOutputStream中FOUT =新的FileOutputStream(thestudent.dat);
      ObjectOutputStream的OOS =新的ObjectOutputStream(FOUT);
      oos.writeObject(学生);
      oos.close();
    }
      赶上(例外五){e.printStackTrace(); }

    }
 

来源:Android的 - 反序列化

 档案文件=新的文件(getExternalFilesDir(空),thestudent.dat);

    尝试 {

      的FileInputStream FINT =新的FileInputStream(文件);
      ObjectInputStream的OIS =新的ObjectInputStream(FINT);
      学生螺柱=(学生)ois.readObject();
      ois.close();
    }
      赶上(例外五){e.printStackTrace(); }

}
 

学生是一类,它实现Serializable接口。在台式机序列化学生thestudent.dat的实例。我把SD卡上文件的Andr​​oid设备,我想反序列化。我收到错误的 java.lang.ClassCastException:javaserializace.Student 。但为什么?我有同样的包序列化,同一个包时,反序列化时。所有所不同的是项目名称。你看到任何解决方案?

编辑 - 学生类来源:

 公共类学生实现Serializable {

私人字符串名称;
私人字符串姓氏;
私人诠释数目;
私人焦性别;
私人诠释年龄;
私人的RC;
私人诠释身份证;

公益助学(){
    ;
}

公众诠释getAge(){
    返回年龄;
}

公共无效setAge(INT岁){
    this.age =年龄;
}

公共字符getGender(){
    返回性别;
}

公共无效setGender(字符性别){
    this.gender =性别;
}

公众诠释的getId(){
    返回ID;
}

公共无效SETID(INT ID){
    this.id = ID;
}

众长getRc(){
    返回RC;
}

公共无效setRc(长RC){
    this.rc = RC;
}

公共字符串的getName(){
    返回名称;
}

公共无效setname可以(字符串名称){
    this.name =名称;
}

公众诠释getNumber(){
    返回数;
}

公共无效setNumber(INT编号){
    this.number =号;
}

公共字符串getSurname(){
    返回姓氏;
}

公共无效setSurname(字符串姓){
    this.surname =姓;
}

}
 

解决方案

我相信,学生在两侧的两个版本不一样的。

由于异常是

java.lang.ClassCastException:javaserializace.Student

这表明Java已经成功地反序列化对象,但同时它类型转换为学生在接收端,它抛出异常,因为类型不一样的。

一个快速的方法来调试这是调用的getClass()收到Student对象和的getName()标签上的接收器Student类。我相信,在这种情况下,这两个是不同的。

和解决方案将是确保在接收端的学生是同一类型。

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.

这篇关于Java序列化 - 反序列化的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆