1

我正在尝试创建一个具有编辑文本视图和按钮的应用程序。此按钮将通过 TCP 连接将编辑文本中的任何内容发送到服务器。

我已经完成了编辑文本的发送,但是在我单击一次按钮后,应用程序崩溃了。我怎样才能把它放到一个循环中,以便我可以发送多条消息?这是我的来源

public class MainActivity extends Activity {

    //Handler h;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText eText = (EditText) findViewById(R.id.address);
        final TextView tView = (TextView) findViewById(R.id.pagetext);
        final Button button = (Button) findViewById(R.id.ButtonGo);
                button.setOnClickListener(new Button.OnClickListener() {
                    public void onClick(View v) {
                        try {
                            Socket s = new Socket("192.168.0.117", 4447);
                            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                            final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                            //send output msg
                            String outMsg = ((EditText)findViewById(R.id.address)).getText().toString().trim();
                            out.write(outMsg);
                            out.flush();
                            Log.i("TcpClient", "sent: " + outMsg);
                        } catch (UnknownHostException e) {
                            tView.setText(e.toString());
                            Log.v("Tcp",e.toString());
                        } catch (IOException e) {
                            tView.setText(e.toString());
                            Log.v("Tcp",e.toString());
                        }catch (Exception e) {
                            tView.setText(e.toString());

                        } 
                    }
                }); 
    }
}
4

1 回答 1

0

我通过使用两个不同的按钮来实现这一点。一个连接按钮和一个发送消息按钮。我认为当服务器尝试重新创建套接字时,TCP 连接中断,服务器发现异常并开始吓坏了。这是新代码。

            bConnect.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    try {
                        Socket s = new Socket("192.168.0.117", 4447);
                        out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    } catch (UnknownHostException e) {
                        tView.setText(e.toString());
                        Log.v("Tcp",e.toString());
                    } catch (IOException e) {
                        tView.setText(e.toString());
                        Log.v("Tcp",e.toString());
                    }catch (Exception e) {
                        tView.setText(e.toString());

                    } 
                }
            });

            bSend.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    try {
                        String outMsg = ((EditText)findViewById(R.id.address)).getText().toString().trim();
                        out.write(outMsg);
                        out.flush();
                        Log.i("TcpClient", "sent: " + outMsg);  
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                    finally{

                    }
                    }


                });
于 2013-01-02T03:03:39.150 回答