Android中提供序列化和反序列化的有两个接口:
Serializable:jdk提供的java接口. IO消耗大.使用简单.(序列化到本地或者通过网络传输)
public class PepBean implements Serializable{ private static final long serialVersionUID = 1238749032; //序列化和反序列化的类的标志. public String name; public int age; public boolean sex; }
直接实现该接口就可以使用.
Parcelable:android sdk 提供的接口. 消耗小 .使用稍微复杂(内存序列化,不能使用在要将数据存储在磁盘上的情况不能能使用在要将数据存储在磁盘上的情况.)
public class PopBean implements Parcelable{ public String name; public int age; public boolean sex; public PopBean(String name,int age,boolean sex){ this.name =name; this.age =age; this.sex =sex; } protected PopBean(Parcel in) { //从序列化后的对象中创建原始对象. name = in.readString(); age = in.readInt(); sex = in.readByte() != 0; } public static final Creator<PopBean> CREATOR = new Creator<PopBean>() { @Override public PopBean createFromParcel(Parcel in) { return new PopBean(in); } @Override public PopBean[] newArray(int size) { return new PopBean[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { //将当前对象写入序列化结果中. dest.writeString(name); dest.writeInt(age); dest.writeByte((byte) (sex ? 1 : 0)); } }
用Android Studio创建起来也很方便. 基本上给你创建好了.
使用:
(1).永久的保存对象数据(将对象数据保存在文件当中,或者是磁盘中) ,用Serializable接口.
PopBean popBean = new PopBean("tom",1,true); //序列化的对象 String sd = Environment.getExternalStorageDirectory().getAbsolutePath(); File file = new File(sd,"aa.text"); //将object对象数据,写入内存文件中 try { ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(file)); oo.writeObject(popBean); oo.close(); } catch (IOException e) { e.printStackTrace(); } //将内存文件中的对象数据,读取出来 try { ObjectInputStream oi = new ObjectInputStream(new FileInputStream(file)); PopBean popBean2 = (PopBean)oi.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }(2).通过序列化操作将对象数据在网络上进行传输(由于网络传输是以字节流的方式对数据进行传输
的.因此序列化的目的是将对象数据转换成字节流的形式) ,用Serializable接口.
ByteArrayOutputStream bo = new ByteArrayOutputStream(); //将对象转换成byte流数据 try { ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(popBean); oo.flush(); oo.close(); } catch (IOException e) { e.printStackTrace(); }//将byte流数据转换成对象 byte[] bb = bo.toByteArray(); ByteArrayInputStream bi = new ByteArrayInputStream(bb); try { ObjectInputStream oi = new ObjectInputStream(bi); PopBean popBean2 = (PopBean)oi.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }----------------------------------socket------------------------
Socket socket =new Socket();//通过sock将对象数据写出
try { OutputStream outputStream = socket.getOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(popBean); objectOutputStream.flush(); objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); }//通过sock将对象数据读取 try { InputStream inputStream = socket.getInputStream(); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); PopBean popBean2 = (PopBean)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }(3).将对象数据在进程之间进行传递(Activity之间传递对象数据时,需要在当前的Activity中对对象
数据进行序列化操作.在另一个Activity中需要进行反序列化操作讲数据取出)
PopBean popBean1 = new PopBean("tom",1,true); //实现Parcelable接口对象PopBean popBean2 = new PopBean("tom",1,true); //实现Serializable接口对象//将序列化数据保存到intent中. Intent intent = new Intent(); intent.putExtra("pop1",popBean1); intent.putExtra("pop2",popBean2); //通过intent,获取序列化的对象数据 intent.getParcelableExtra("pop1"); intent.getSerializableExtra("pop2");(5).Bundle包装数据传递.
//传递数据
Intent intent = new Intent(MainActivity.this,MyService1.class); PopBean popBean = new PopBean("",1,true); Bundle bundle = new Bundle(); bundle.putParcelable("b",popBean); intent.putExtra("name","haha"); intent.putExtra("bund",bundle); startService(intent);//获取数据 PopBean popBean1 = (PopBean) intent.getBundleExtra("bund").getParcelable("b");