首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android网络传输的传接对象

2012-07-18 
android网络传输的传送对象此方法将对象写入对象流,然后转换成string,故适用于http和soap协议或者其他的网

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);
?

?

热点排行