我正在开发一个使用https://github.com/Gottox/socket.io-java-client实现socket io客户端的android应用程序。
我的Nodejs服务器
var app = require('http').createServer(),
io = require('socket.io').listen(app),
http = require('http'),
mysql = require('mysql'),
connectionsArray = [],
connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'login',
port: 3306
});
app.listen(3000);
io.sockets.on('connection', function (socket) {
socket.on('register_usernames', function (data) {
io.sockets.socket(socket.id).emit('register_usernames' , {username:"one"});
io.sockets.socket.(socket.id).emit('register_usernames_one', {username:"two"});
getBookingHistory(data);
});
connectionsArray.push(socket);
});
Android客户端
package com.nuappz.socketdemo;
import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import java.net.MalformedURLException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Search extends Activity implements IOCallback{
SocketIO socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
// Try making the socket connection
socket = new SocketIO("http://nuappzdev.weteksi.com:3000/");
socket.connect(this);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
socket.emit("register_usernames", "123");
}
@Override
public void onDisconnect() {
// TODO Auto-generated method stub
}
@Override
public void onConnect() {
// TODO Auto-generated method stub
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
// TODO Auto-generated method stub
}
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
// TODO Auto-generated method stub
}
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
// TODO Auto-generated method stub
if(event.equals("register_usernames")) {
Log.d("message response", args.toString());
} else if (event.equals("register_usernames_one")) {
Log.d("message reply", args.toString());
}
}
@Override
public void onError(SocketIOException socketIOException) {
// TODO Auto-generated method stub
}
}
在接收事件时的方法中,它显示args。但它显示以下错误
Logcat
06-09 18:25:52.321:I/io.socket(1890):>5:::{"args": ["123"],"name":"register_usernames"}
06-09 18:25:52.322: I/io.socket(1890): < 1::
06-09 18:25:52.408: I/io.socket(1890): < 5:::{"name":"message_response","args": [{"username":"one"}]}
06-09 18:25:52.409: D/message response(1890): [Ljava.lang.Object;@4177d208
06-09 18:25:52.409: I/io.socket(1890): < 5:::{"name":"message_reply","args": [{"username":"two"}]}
06-09 18:25:52.409: D/message reply(1890): [Ljava.lang.Object;@41739f30
06-09 18:25:52.471: D/dalvikvm(1890): threadid=16: interp stack at 0x5d914000
1.如何发送有关发射的数据以及如何从客户端获取数据 2.如何发送数组或Json并解析它。
答案 0 :(得分:0)
我发现了真正的问题是什么 因为我已经尝试在持有数组的对象中获取值 对象本身就是一个数组。所以
我的尝试是:
args.toString();
对象无法转换为字符串。
我实现了这个目标:
args[0].toString(); //args[position].toString();
因此我如何解决了我的问题..