遇到一个Android的socket问题.求解答
1个回答
2015-08-23 · 知道合伙人宠物行家
关注
展开全部
new Thread() {
public void run() {
Socket socket = null;
BufferedReader br = null;
try {
socket = new Socket("192.168.1.129", 1290);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
info = "is connected";
handler.sendEmptyMessage(0);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
char[] buffer = new char[256];
while(true){
try {
i = br.read(buffer);
info = new String(buffer);
handler.sendEmptyMessage(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
while(true){}死循环了,不知道你的本意是不是想一直读数据,然后去更新显示?
提出建议:
把内容全部读出来,可以使用StringBuffer或StringBuild,然后再刷新显示
Socket使用以后要记得close
public void run() {
Socket socket = null;
BufferedReader br = null;
try {
socket = new Socket("192.168.1.129", 1290);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
info = "is connected";
handler.sendEmptyMessage(0);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
char[] buffer = new char[256];
while(true){
try {
i = br.read(buffer);
info = new String(buffer);
handler.sendEmptyMessage(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
while(true){}死循环了,不知道你的本意是不是想一直读数据,然后去更新显示?
提出建议:
把内容全部读出来,可以使用StringBuffer或StringBuild,然后再刷新显示
Socket使用以后要记得close
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询