android socket传输序列化对象的代码
将做工程过程中常用的内容做个珍藏,如下代码段是关于android socket传输序列化对象的代码。
<p>package com;</p><p>import java.io.Serializable;</p><p>public class Message implements Serializable{
String score;
public Message(String uId, String score) {
this.uId = uId;
this.score = score;
}
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
</p>
主要是改变传送的方式
ObjectOutputStream os= null;
ObjectInputStream in =null;
这里的片段
os = new ObjectOutputStream(mSocket.getOutputStream());
in = new ObjectInputStream(new BufferedInputStream(mSocket.getInputStream()));
读取
Object obj=null;
if((obj=in.readObject())!=null)
{
com.Message mes=(com.Message)obj;
}
写入
os=new ObjectOutputStream(client.getOutputStream());
os.writeObject(new com.Message("user1:",mStrMSG));
os.flush();
不懂的可以先看看socket有关的。