通过tcp ip在按钮内发送文本

时间:2012-07-06 14:57:04

标签: android eclipse button text tcp

我想将一些文本发送到服务器。我想要的是按钮内的文字。我的意思是按钮包含要发送到服务器的文本

这是我的activity.java

textout = (TextView)findViewById(R.id.textout);
    Button buttonOK = (Button)findViewById(R.id.button1);
    Button buttonYES = (Button)findViewById(R.id.button2);

    buttonOK.setOnClickListener(buttonSendOnClickListener);
}
    Button.OnClickListener buttonSendOnClickListener = new Button.OnClickListener(){
        @SuppressWarnings("null")
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Socket socket = null;
            DataOutputStream dataOutputStream = null;
            DataInputStream dataInputStream = null;

            OutputStream out = null;
            try {
                socket = new Socket("192.168.43.15", 8888);
                dataOutputStream = new DataOutputStream(socket.getOutputStream());
                dataInputStream = new DataInputStream(socket.getInputStream());
                OutputStream out1 = socket.getOutputStream();      

                TextView textout = null;
                textout.setText(dataInputStream.readUTF());             
                textout.setText("OKAY");            

这是我的server.java

public static void main(String[] args){
  ServerSocket serverSocket = null;
  Socket socket = null;
  DataInputStream dataInputStream = null;
  DataOutputStream dataOutputStream = null;
 try {
   serverSocket = new ServerSocket(8888);
   System.out.println("Listening :8888");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  while(true){
   try {
    socket = serverSocket.accept();
    dataInputStream = new DataInputStream(socket.getInputStream());
    dataOutputStream = new DataOutputStream(socket.getOutputStream());
    System.out.println("ip: " + socket.getInetAddress());
    System.out.println("message: " + dataInputStream.readUTF());
    dataOutputStream.writeUTF("Hello!");

现在,当我点击确定按钮时,我遇到问题,文本将不会显示在我的服务器中。但客户端和服务器之间的连接已经建立。突然我的应用程序关闭了。

这是logcat显示

07-06 22:06:54.880: I/dalvikvm(590): threadid=3: reacting to signal 3
07-06 22:06:54.936: I/dalvikvm(590): Wrote stack traces to '/data/anr/traces.txt'

这是我的服务器显示

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at MyServer.main(MyServer.java:31)

我该怎么做才能让文字显示在我的服务器中。 请帮我。非常感谢。

1 个答案:

答案 0 :(得分:0)

你实际上从未发送过文字。我会尝试 doas.write(text.getBytes())首先......