我对安卓是新手,需要简单的客户端服务器(网络中的本地服务器)的http连接代码在安卓application.the连接开始时,应用程序启动,如果有任何更新,它应该通知在客户端和服务器的响应必须是基于客户端的请求。请帮帮忙。谢谢
发布于 2010-07-29 14:28:49
Socket socket;
InputStream is;
OutputStream os;
String hostname;
int port;
public void connect() throws IOException {
socket = new Socket(hostname, port);
is = socket.getInputStream();
os = socket.getOutputStream();
}
public void send(String data) throws IOException {
if(socket != null && socket.isConnected()) {
os.write(data.getBytes());
os.flush();
}
}
public String read() throws IOException {
String rtn = null;
int ret;
byte buf[] = new byte[512];
while((ret = is.read(buf)) != -1) {
rtn += new String(buf, 0, ret, "UTF-8");
}
return rtn;
}
public void disconnect() throws IOException {
try {
is.close();
os.close();
socket.close();
} finally {
is = null;
os = null;
socket = null;
}
}
连接、发送、读取、断开连接:)
https://stackoverflow.com/questions/3343542
复制相似问题