Android 我的客户端将连接到服务器,但仅此而已

Android 我的客户端将连接到服务器,但仅此而已,android,client,dataoutputstream,Android,Client,Dataoutputstream,Android客户端向服务器发送字符串。服务器将确认来自设备的连接,并在正确的端口上进行连接,但仅此而已。应该发生的是,字符串打印在服务器控制台上 作为参考,我创建了完全相同的客户端,没有在android应用程序中运行它,它工作得很好,这让我相信我在android方面遗漏了一些东西。有谁能提出解决这个问题的建议吗。非常感谢 客户端代码: public class ObjectTestActivity extends Activity { Button submit; TextView tv;

Android客户端向服务器发送字符串。服务器将确认来自设备的连接,并在正确的端口上进行连接,但仅此而已。应该发生的是,字符串打印在服务器控制台上

作为参考,我创建了完全相同的客户端,没有在android应用程序中运行它,它工作得很好,这让我相信我在android方面遗漏了一些东西。有谁能提出解决这个问题的建议吗。非常感谢

客户端代码:

public class ObjectTestActivity extends Activity {

Button submit;
TextView tv;
private String name = "Hello Android";
private DataOutputStream dos;
private DataInputStream dis;
private final int PORT = 3000;

Button send;
InetAddress host;


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    send = (Button) findViewById(R.id.send);
    tv = (TextView) findViewById(R.id.tv);


    try{

        host = InetAddress.getLocalHost();
        Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

        dos = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());

    }catch(UnknownHostException e){}
     catch(IOException e){}
}


public void onClick(View view){

    try{
        dos.writeUTF(name);
        dos.flush();
        dis.close();
        dos.close();
    }catch(IOException e){}
}

onClick附加到什么?尝试更改为:

public class MyActivity extends Activity {

  Button submit;
  TextView tv;
  private String name = "Hello Android";
  private DataOutputStream dos;
  private DataInputStream dis;
  private final int PORT = 3000;

  Button send;
  InetAddress host;

  protected void onCreate(Bundle icicle) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

        send = (Button) findViewById(R.id.send);
        tv = (TextView) findViewById(R.id.tv);


        try{

            host = InetAddress.getLocalHost();
            Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

            dos = new DataOutputStream(socket.getOutputStream());
            dis = new DataInputStream(socket.getInputStream());

       }catch(UnknownHostException e){}
        catch(IOException e){}


        send.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 try{
                 dos.writeUTF(name);
                 dos.flush();
                 dis.close();
                 dos.close();
              }catch(IOException e){}
             }
         });
     }
 }
对于按钮onClick事件

简单地说:在onCreate(send.onCreate(…)中定义按钮onClick方法


这个例子来自于什么是onClick?尝试更改为:

public class MyActivity extends Activity {

  Button submit;
  TextView tv;
  private String name = "Hello Android";
  private DataOutputStream dos;
  private DataInputStream dis;
  private final int PORT = 3000;

  Button send;
  InetAddress host;

  protected void onCreate(Bundle icicle) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

        send = (Button) findViewById(R.id.send);
        tv = (TextView) findViewById(R.id.tv);


        try{

            host = InetAddress.getLocalHost();
            Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

            dos = new DataOutputStream(socket.getOutputStream());
            dis = new DataInputStream(socket.getInputStream());

       }catch(UnknownHostException e){}
        catch(IOException e){}


        send.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 try{
                 dos.writeUTF(name);
                 dos.flush();
                 dis.close();
                 dos.close();
              }catch(IOException e){}
             }
         });
     }
 }
对于按钮onClick事件

简单地说:在onCreate(send.onCreate(…)中定义按钮onClick方法


这个例子来自

杰克,谢谢。我总是将onclick声明为一个单独的方法。我觉得奇怪,这会产生如此大的影响,但你的解决方案已经解决了我两个多星期的问题。谢谢你,杰克,谢谢你。我总是将onclick声明为一个单独的方法。我觉得奇怪,这会产生如此大的影响,但你的解决方案已经解决了我两个多星期的问题。非常感谢。