持续发送消息

时间:2013-01-10 06:32:14

标签: android client-server

我正在开发一个应用程序,其中客户端向服务器发送消息,服务器接收消息。但是在我发送消息的应用程序中,它只被服务器接收一次。如果我必须再次向服务器发送消息,我必须强制关闭应用程序,然后再次发送消息。以连续方式表示我必须将数据发送到服务器。以下是我的代码。请帮我解决这个问题。

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;

import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity {
TextView serverMessage;
Thread m_objThreadClient;
Socket clientSocket;
private boolean connected = false;

EditText et;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        serverMessage=(TextView)findViewById(R.id.textView1);
        et=(EditText)findViewById(R.id.editText1);

    }



  public void Start(View view)
{
     // m_objThreadClient.start();
    m_objThreadClient=new Thread(new Runnable() {
        public void run()
           {

              try
               {

                 clientSocket= new Socket("192.168.0.103",2000);
                 ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
                 oos.writeObject(et.getText().toString());
                 Message serverMessage= Message.obtain();
                 ObjectInputStream ois =new ObjectInputStream(clientSocket.getInputStream());
                 String strMessage = (String)ois.readObject();
                 serverMessage.obj=strMessage;
                 mHandler.sendMessage(serverMessage);

                 oos.close();
                 ois.close();

              }

              catch(Exception e)
                     {
                         e.printStackTrace();
                     }


          }
            });

    m_objThreadClient.start();
    //m_objThreadClient.resume();

}
Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        messageDisplay(msg.obj.toString());
    }
};
public void messageDisplay(String servermessage)
{
    serverMessage.setText(""+servermessage);
    m_objThreadClient.start();
}

}

2 个答案:

答案 0 :(得分:0)

再次调用start函数时,可以绑定套接字。 验证这种情况。

 if(clientSocket != null && clientSocket.isConnected)clientSocket.close();
  clientSocket= new Socket("192.168.0.103",2000);

答案 1 :(得分:0)

首先将clientSocket对象设为本地。

然后,为了解决循环问题,试试这个:

调用函数starteMessageThreadRunner,它将启动将消息发送到服务器的线程,一旦runnable完成发送数据,它将再次调用处理程序,然后处理程序将调用该线程试。

    /**
         * start the thread and handler which will send the message
         */
        public void starteMessageThreadRunner() {

            Message message = handler.obtainMessage(1, "");
            handler.sendMessage(message);
        }

        /**
         * handler for calling the thread.
         */
        Handler handler = new Handler() {

            public void handleMessage(Message msg) {
                // do something with the response received (if you want to)
                 serverMessage.setText(msg.obj.toString());
                //this line will call runnable again and will send the message to the server.
                handler.post(runnable);

            };
        };


        /**
         * A runnable object for calling the handler.
         */
        private Runnable runnable = new Runnable() {

            @Override
            public void run() {

                // send the message to the server.

try
               {

                 Socket clientSocket= new Socket("192.168.0.103",2000);
                 ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
                 oos.writeObject(et.getText().toString());
                 Message serverMessage= Message.obtain();
                 ObjectInputStream ois =new ObjectInputStream(clientSocket.getInputStream());


                 String strMessage = (String)ois.readObject();
Message message = handler.obtainMessage(1, strMessage );

                 oos.close();
                 ois.close();
handler.sendMessage(message);
              }

              catch(Exception e)
                     {
                         e.printStackTrace();
                     }



            }
        };

如果您想退出此流程,请致电:

handler.removeCallbacks(runnable);

它将删除消息队列中任何待处理的Runnable r帖子。

希望这有帮助!