'Android and Socket.io , send and receive data
I can send data to server.js and it work well but i can't receive data from server.When i click button server.js gets data i think Server.js send data but my App need some code to get data.
My server.js work well
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});
io.on('connection',function(socket){
console.log('one user connected '+socket.id);
socket.on('CHAT' , function (data) {
console.log('======CHAT message========== ');
console.log(data);
socket.emit('CHAT',data);
});
socket.on('disconnect',function(){
console.log('one user disconnected '+socket.id);
});
})
http.listen(3000,function(){
console.log('server listening on port 3000');
})
And my Activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.Socket;
import java.net.URISyntaxException;
import io.socket.client.IO;
import io.socket.emitter.Emitter;
public class MainActivity extends AppCompatActivity {
public Button button;
public TextView text;
public EditText edit;
private io.socket.client.Socket mSocket;
{
try {
mSocket = IO.socket("http://192.168.1.4:3000");
} catch (URISyntaxException e) {
Log.d("myTag", e.getMessage());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.text);
edit = (EditText) findViewById(R.id.edit);
mSocket.connect();
text.setText("");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String message = edit.getText().toString().trim();
edit.setText("");
if (!message.isEmpty()) {
//send message
String jsonString = "{message: " + "'" + message + "'" + "}";
try {
JSONObject jsonData = new JSONObject(jsonString);
mSocket.emit("CHAT", jsonData);
} catch (JSONException e) {
Log.d("me", "error send message " + e.getMessage());
}
}
}
});//button.setOnClickListener
}//on create
}//end of class
This code need something like this to get data from Server.js , i do not know how can i use it
mSocket.on("CHAT", new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
JSONObject messageJson = new JSONObject(args[0].toString());
String message = messageJson.getString("message");
text.setText(message);
runOnUiThread(new Runnable() {
@Override
public void run() {
// mMessageAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Your toast message.", Toast.LENGTH_SHORT).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Solution 1:[1]
This code work well. it is a real simple code to send and receive data between nodejs and android.
public class MainActivity extends AppCompatActivity {
public Button button;
public TextView text;
public EditText edit;
public String message;
private io.socket.client.Socket mSocket;
{
try {
mSocket = IO.socket("http://192.168.1.4:3000");
} catch (URISyntaxException e) {
Log.d("myTag", e.getMessage());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.text);
edit = (EditText) findViewById(R.id.edit);
mSocket.connect();
text.setText("");
setListening();
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String message = edit.getText().toString().trim();
edit.setText("");
if (!message.isEmpty()) {
//send message
String jsonString = "{message: " + "'" + message + "'" + "}";
try {
JSONObject jsonData = new JSONObject(jsonString);
mSocket.emit("CHAT", jsonData);
} catch (JSONException e) {
Log.d("me", "error send message " + e.getMessage());
}
}
}
});
}//on create
private void setListening() {
mSocket.on("CHAT", new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
JSONObject messageJson = new JSONObject(args[0].toString());
message = messageJson.getString("message");
runOnUiThread(new Runnable() {
@Override
public void run() {
text.setText(message);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}//end of class
Solution 2:[2]
Perhaps is late but you have a mistake in your code.
socket.on('CHAT' , function (data) {
console.log('======CHAT message========== ');
console.log(data);
socket.emit('CHAT',data);
});
socket.off('disconnect',function(){
console.log('one user disconnected '+socket.id);
});
When disconnecting, you are calling to socket.on instead socket.off. I fixed in the code above.
Solution 3:[3]
Check you socket version in gradle, I resolved the issue by downgrading from
2.0.0 to 0.8.3
implementation ('io.socket:socket.io-client:0.8.3') {
exclude group: 'org.json', module: 'json'
}
and it worked for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Moh_beh |
Solution 2 | Manuel Rodriguez |
Solution 3 | Osama Zafar |