对象的序列化和反序列化
当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个Java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象。
把Java对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为Java对象的过程称为对象的反序列化。
对象的序列化主要有两种用途:
1)把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;
2)在网络上传送对象的字节序列。
在用intent传递对象之前,要将对象序列化(基本类型和String已经实现序列化)要想传递自定义的对象就有序列化。序列化有两种方式
1、实现Serializable接口
1 import java.io.Serializable; 2 3 public class Student implements Serializable { 4 private String name; 5 int age; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public int getAge() { 16 return age; 17 } 18 19 public void setAge(int age) { 20 this.age = age; 21 } 22 23 }
2实现Parcelable接口
import android.os.Parcel; import android.os.Parcelable; /** * @author ypeng*/ public class DishCom implements Parcelable { private String id; private String res_id; private String dish_id; private String user_id; private String score; private String comment; private String recommend; private String reply; private String picture; private String create_time; public DishCom() { }; public DishCom(String id, String res_id, String dish_id, String user_id, String score, String comment, String recommend, String reply, String picture, String create_time) { super(); this.id = id; this.res_id = res_id; this.dish_id = dish_id; this.user_id = user_id; this.score = score; this.comment = comment; this.recommend = recommend; this.reply = reply; this.picture = picture; this.create_time = create_time; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getRes_id() { return res_id; } public void setRes_id(String res_id) { this.res_id = res_id; } public String getDish_id() { return dish_id; } public void setDish_id(String dish_id) { this.dish_id = dish_id; } public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getRecommend() { return recommend; } public void setRecommend(String recommend) { this.recommend = recommend; } public String getReply() { return reply; } public void setReply(String reply) { this.reply = reply; } public String getPicture() { return picture; } public void setPicture(String picture) { this.picture = picture; } public String getCreate_time() { return create_time; } public void setCreate_time(String create_time) { this.create_time = create_time; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel parcel, int flags) { // TODO Auto-generated method stub parcel.writeString(this.id); parcel.writeString(this.res_id); parcel.writeString(this.dish_id); parcel.writeString(this.user_id); parcel.writeString(this.score); parcel.writeString(this.comment); parcel.writeString(this.recommend); parcel.writeString(this.reply); parcel.writeString(this.picture); parcel.writeString(this.create_time); } public static final Parcelable.Creator<DishCom> CREATOR = new Creator<DishCom>() { public DishCom[] newArray(int size) { return new DishCom[size]; } public DishCom createFromParcel(Parcel parcel) { return new DishCom(parcel.readString(), parcel.readString(), parcel.readString(), parcel.readString(), parcel.readString(), parcel.readString(), parcel.readString(), parcel.readString(), parcel.readString(), parcel.readString()); } }; }
其他:
1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但 在这种情况下,还是建议你用Serializable 。