本文介绍了Android和Socket.io,发送和接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以将数据发送到 server.js 并且它运行良好,但我无法从服务器接收数据.当我单击按钮 server.js 获取数据时,我认为 Server.js 发送数据但我的应用程序需要一些代码来获取数据.
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.
我的 server.js 运行良好
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');
})
还有我的活动:
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
这段代码需要这样的东西才能从 Server.js 获取数据,我不知道如何使用它
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();
}
}
});
推荐答案
此代码运行良好.在nodejs和android之间发送和接收数据是一个真正简单的代码.
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
这篇关于Android和Socket.io,发送和接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!