在RunableThread接口中, out.println(message);这一句,我如果直接运行的话,服务器端收不到文件,但是我如果在 out.println(message);暂停一下再运行可以接受到,请问这个是怎么回事的??
public class MainActivity extends ActionBarActivity {
private EditText editText;
private Button button;
private TextView MTextview;
MyHandler handler=new MyHandler();
RunnableThread runnable = new RunnableThread();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MTextview=(TextView) findViewById(R.id.TextView2);
editText = (EditText)findViewById(R.id.editText1);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(runnable).start();
}
});
public class RunnableThread implements Runnable{
public void run() {
Socket socket = null;
String message = editText.getText().toString()+ "\r\n" ;
try {
// MTextview.setText("1122");
//创建客户端socket,注意:不能用localhost或127.0.0.1,Android模拟器把自己作为localhost
socket = new Socket("172.16.5.30",8000);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter
(socket.getOutputStream())),true);
//发送数据
out.println(message);
//接收数据
/* BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = in.readLine();
if (null != msg){
System.out.println(msg);
}
else{
// editText.setText("data error");
}*/
out.close();
// in.close();
Message msg1 = new Message();
Bundle b = new Bundle();// 存放数据
b.putString("text_v", "1112");
msg1.setData(b);
MainActivity.this.handler.sendMessage(msg1);
} catch (UnknownHostException e) {
// e.printStackTrace();
}
catch (IOException e) {
// e.printStackTrace();
}
finally{
try {
if (null != socket){
socket.close();
}
} catch (IOException e) {
// e.printStackTrace();
}
}
public class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper L) {
super(L);
}
// 子类必须重写此方法,接受数据
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
// 此处可以更新UI
Bundle b = msg.getData();
String color = b.getString("text_v");
MainActivity.this.MTextview.setText(color);
}
}
public class MainActivity extends ActionBarActivity {
private EditText editText;
private Button button;
private TextView MTextview;
MyHandler handler=new MyHandler();
RunnableThread runnable = new RunnableThread();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MTextview=(TextView) findViewById(R.id.TextView2);
editText = (EditText)findViewById(R.id.editText1);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(runnable).start();
}
});
public class RunnableThread implements Runnable{
public void run() {
Socket socket = null;
String message = editText.getText().toString()+ "\r\n" ;
try {
// MTextview.setText("1122");
//创建客户端socket,注意:不能用localhost或127.0.0.1,Android模拟器把自己作为localhost
socket = new Socket("172.16.5.30",8000);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter
(socket.getOutputStream())),true);
//发送数据
out.println(message);
//接收数据
/* BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = in.readLine();
if (null != msg){
System.out.println(msg);
}
else{
// editText.setText("data error");
}*/
out.close();
// in.close();
Message msg1 = new Message();
Bundle b = new Bundle();// 存放数据
b.putString("text_v", "1112");
msg1.setData(b);
MainActivity.this.handler.sendMessage(msg1);
} catch (UnknownHostException e) {
// e.printStackTrace();
}
catch (IOException e) {
// e.printStackTrace();
}
finally{
try {
if (null != socket){
socket.close();
}
} catch (IOException e) {
// e.printStackTrace();
}
}
public class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper L) {
super(L);
}
// 子类必须重写此方法,接受数据
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
// 此处可以更新UI
Bundle b = msg.getData();
String color = b.getString("text_v");
MainActivity.this.MTextview.setText(color);
}
}