当然,我们要自己实现 对象 <--> byte[](字节数组) 的转换,因为j2ME没有那么强大的api,移动设备不允许。
序列化对象:
package po;
import javaioByteArrayOutputStream;
import javaioDataOutputStream;
import javaioIOException;
public class LoginUser {
private String name;
private String password;
public LoginUser(String name, String pwd) {
thisname = name;
thispassword = pwd;
}
public byte[] toBytes() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
doswriteUTF(thisgetName());
doswriteUTF(thisgetPassword());
return baostoByteArray();
} catch (Exception e) {
eprintStackTrace();
} finally {
try {
baosclose();
} catch (IOException ex) {
exprintStackTrace();
}
try {
dosclose();
} catch (IOException ex) {
exprintStackTrace();
}
}
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
}
信息接收线程
public class GeneralReceiver {
>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)