当前位置: 代码迷 >> Android >> 安卓TCP通讯客户端接受数据的有关问题
  详细解决方案

安卓TCP通讯客户端接受数据的有关问题

热度:75   发布时间:2016-04-28 05:00:14.0
安卓TCP通讯客户端接受数据的问题。
我想让手机和电脑通讯,电脑是服务器,手机是客户端。做出来后发现客户端收不到(收到了不显示?)服务器发出的信息。其他一切正常,求这是什么情况?
客户端代码如下
public class MainActivity extends Activity{
private Button button1 = null;
private Button button2 = null;
private Button button3 = null;
private EditText edit1 = null;
private EditText edit2 = null;
private static EditText edit3 = null;
private int  ZT=0;
public Socket socket=null;
private String msg=null;
public PrintWriter out=null;
public BufferedReader br=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
edit1=(EditText)findViewById(R.id.edit1);
edit2=(EditText)findViewById(R.id.edit2);
edit3=(EditText)findViewById(R.id.edit3);
button1.setOnClickListener(new listener());
button2.setOnClickListener(new listener());
button3.setOnClickListener(new listener());
}
class listener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
//按钮1按下,启动通讯线程。
if(v.getId()==R.id.button1){
if(ZT==0){
ZT=1;
 try
                {
                    _thread.start();
                }
                catch (Exception e)
                {
                    e.printStackTrace();  
                }
}
}
//按钮2按下,发送信息。
else if(v.getId()==R.id.button2){
msg=edit3.getText().toString();
edit3.setText(null);
edit3.setText(null);
try
        { 
            out.println(msg);
            out.flush();
        }
        catch (Exception e)
        {
         e.printStackTrace();
        }
}
//按钮3按下,关闭程序
else if(v.getId()==R.id.button3){
finish();
}
}

}
 private Thread _thread = new Thread(){
 public void run(){
 try {
 //获取控件里填写的IP地址和端口号,连接服务器
socket = new Socket(edit1.getText().toString(),Integer.parseInt(edit2.getText().toString()));
//获得输入输出流
out =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
}  catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
 String in=null;
 Message message=null;
 Bundle bundle=null;
 message = new Message();
 bundle = new Bundle();
 while(true){
 try {
in=br.readLine();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
 //判断是否有信息到达,如果有,取得信息,传递参数。
 if(in!=null){
 bundle.putString("mesg",in);
 message.setData(bundle);
 in=null;
 handler.sendMessage(message);
 }

}

 }
 };
 public Handler handler = new Handler(){
 public void handleMessage(Message msg){
 super.handleMessage(msg);
 try{
 //更新控件
 MainActivity.edit3.setText(msg.getData().getString("mesg"));
 }
 catch (Exception e)
         {
                
         } 
 }
 };
 
}


布局文件我就不发了,直接贴界面的图片。


求大神解答!
------解决方案--------------------
可能br.readLine();
out.write();
这段代码有问题;
public PrintWriter out;
 public BufferedReader br;
使用着两个类进行读写我至今也没弄明白要怎么才能写对。
建议你直接用input 或者outputstream进行读写。
比如当OutputStream out  = socket.getOutputStream();
  相关解决方案