Android socket编程,用线程来捕获socket返回的数据,出现了问题,没有 5
mHandler=newHandler(){publicvoidhandleMessage(Messagemsg){EditText.setText(msg.obj.to...
mHandler = new Handler() {
public void handleMessage(Message msg) {
EditText.setText(msg.obj.toString());
/*switch (msg.what) {
case 0:
Toast.makeText(getApplicationContext(),
"gfsakjgfskjgfskjfgsjk", Toast.LENGTH_SHORT).show();// 显示时间较
EditText.setText("失去了" + msg.obj + "秒");
break;
default:
break;
}*/
}
};
thread = new Thread(new Runnable() {
public void run() {
try {
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String buffer = "";
while ((lineString = bufferedReader.readLine().toString()) != null) {
// "asasada",Toast.LENGTH_SHORT).show();//
buffer = lineString.toString() + buffer;
}
Message msg = new Message();
msg.obj = buffer;
msg.what = 0;
mHandler.sendMessage(msg);// 发送消息
} catch (IOException e) {
}
问题似乎是出在线程里的bufferedReader = new BufferedReader(newInputStreamReader(socket.getInputStream()));没有取到数据,主机实际上已经发了在socket的,hanlder用来刷新控件中的数据,bufferedReader没有数据,所以后面控件就没有更新了,求解决问题,谢谢 展开
public void handleMessage(Message msg) {
EditText.setText(msg.obj.toString());
/*switch (msg.what) {
case 0:
Toast.makeText(getApplicationContext(),
"gfsakjgfskjgfskjfgsjk", Toast.LENGTH_SHORT).show();// 显示时间较
EditText.setText("失去了" + msg.obj + "秒");
break;
default:
break;
}*/
}
};
thread = new Thread(new Runnable() {
public void run() {
try {
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String buffer = "";
while ((lineString = bufferedReader.readLine().toString()) != null) {
// "asasada",Toast.LENGTH_SHORT).show();//
buffer = lineString.toString() + buffer;
}
Message msg = new Message();
msg.obj = buffer;
msg.what = 0;
mHandler.sendMessage(msg);// 发送消息
} catch (IOException e) {
}
问题似乎是出在线程里的bufferedReader = new BufferedReader(newInputStreamReader(socket.getInputStream()));没有取到数据,主机实际上已经发了在socket的,hanlder用来刷新控件中的数据,bufferedReader没有数据,所以后面控件就没有更新了,求解决问题,谢谢 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询