Android socket 编程 客户端接收的数据数据无法更新到UI上,只有当客户端与服务器断开连接时才能更新到UI上
public class MianActivity extends Activity {
private TextView textView;
public Handler myHandler = new Handler()
{
public void handleMessage(Message msg)
{
Bundle bundle = msg.getData();
String str = bundle.getString("mess");
textView.setText(str);
};
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) this.findViewById(R.id.text_show);
startGetData();
}
private void startGetData()
{
new Thread(new Runnable()
{
@Override
public void run()
{
Socket socket;
try
{
socket = new Socket();
socket.connect(new InetSocketAddress("192.168.191.1", 7777), 5000);
InputStream in = socket.getInputStream();
byte[] buffer = new byte[200];
String data = "";
int length = 0;
while(-1 !=(length = in.read(buffer,0,buffer.length)) )
{
data += new String(buffer,0,length);
}
Message msg = new Message();
Bundle bu = new Bundle();
bu.clear();
bu.putString("mess", data);
msg.setData(bu);
myHandler.sendMessage(msg);
in.close();
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
}
}
请高手指点一下那里问题?