android网络传输的传送对象

此方法将对象写入对象流,然后转换成string,故适用于http和soap协议或者其他的网络传输。
注意:对象须实现Serializable接口,定义的serialVersionUID相同,列表传送也需实现Serializable接口,hashtable可以。
服务器端写对象:
try {Hashtable<integer user>hashtable=new Hashtable<integer user>();
                                        ByteArrayOutputStream  baos=new ByteArrayOutputStream();
                                        ObjectOutputStream objout = null;
                                        objout = new ObjectOutputStream(baos);
                                        objout.writeObject(hashtable);//写对象
                                        byte[] b=baos.toByteArray();
                                        String s=new BASE64Encoder().encode(b);
                                        objout.close();
                                        baos.close();
                                        return s;
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                        return "erroe";
                                }

客户端解析对象:
byte[] b=new BASE64Decoder().decodeBuffer(s);//s是接受的对象
                        ByteArrayInputStream bais=new ByteArrayInputStream(b);
                        ObjectInputStream ios=new ObjectInputStream(bais);
                        Hashtable<integer user>hashtable=new Hashtable<integer user>();
                        hashtable=(Hashtable<integer user>) ios.readObject();
                        //User user =(User) ios.readObject();
                        System.out.println(hashtable.size());
                        System.out.println(hashtable);</integer></integer></integer></integer></integer>

你可能感兴趣的:(android)