Note that there are some explanatory texts on larger screens.

plurals
  1. POSend message continuously
    text
    copied!<p>I am developing an application in which client sends messages to server and server receives messages. But in my application when I am sending message, it is received by server only once. If I have to send message again to server I have to Force close the application and then again send message. Means in continuous manner I have to send data to server. Below is my code. Please help me to solve this problem.</p> <pre><code>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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload