Reputation: 31
I am trying to implement a simple chat app between a java-pc server and an android app.
I have mixed a few tutorials found on line to come up with an almost satisfying results.
My problem is the following: for some reason I can receive message to my server, but when I send it from my server (the pc), my android phone can't read the string.
Here is the part of in the java-pc program sending the message: First the sending button method:
public void sendMessageBtn(){
String message = userText.getText();
userText.setText("");
chatWindow.appendText("Server : "+ message + "\n");
// send the message to the client
mServer.sendMessage(message);
}
And here is the sendMessage(message) method in my ServerClass:
public void sendMessage(String message){
System.out.println("Message blocked here");
if(mOut != null){
System.out.println("Message : " + message + " sent");
mOut.println();
mOut.flush();
System.out.println("Message fully sent");
}
}
Everything seems to work on that side as I receive all the println();
On the phone side here is the method overwriting the listener from my asynk class:
public class connectTask extends AsyncTask<String, String, TCPClient>{
@Override
protected TCPClient doInBackground(String... message) {
// We crape a tpc client object and ...
mTcpClient = new TCPClient(new OnMessageReceived() {
// ... we implement the messageReceived previously defined as an interface
@Override
public void messageReceived(String message) {
// this method calls the onProgressUpdate
Log.e("messagedReceive method :", message);
publishProgress(message);
// Toast.makeText(getApplicationContext(),serverMessage,Toast.LENGTH_LONG).show();
}
});
mTcpClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
// Log.e("On progressedUpdate", "super passed");
// in the arrayList we add the message from the server
// arrayList.add("Message received");
Log.e("MainActivity: ", "onProgressedUpdate call");
arrayList.add("Server: "+values[0]);
// notify the adapter that the data set has changed. This means that new message received
// from server was added to the list
mAdapter.notifyDataSetChanged();
}
}
From what I understood, it feels like the value[0] is empty eventhough the message is received or at least one message is as all my Log.e are activated, indicating that the method onReceive has been activated.
Thanks in advance for your help, I have been working on this for some time now and starting to become desperate ;).
Kind regards, A
Upvotes: 1
Views: 92
Reputation: 5950
There is problem in your server side code.
Check your sendMessage
method.
You didn't write message to OutputStream
mOut
. so do it in if
block.
if(mOut != null) {
System.out.println("Message : " + message + " sent");
mOut.pritnln(message);
mOut.println();
mOut.flush();
System.out.println("Message fully sent");
}
Upvotes: 1