2014-06-09 127 views
0

我正在開發一個使用https://github.com/Gottox/socket.io-java-client實現套接字io客戶端的android應用程序。Android套接字io客戶端節點js不能獲取值

我的服務器的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 
    } 
}  

在方法上,當事件recevied它顯示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.How我可以發送關於EMIT以及如何從客戶端得到它的數據。
2.如何發送數組或Json並解析它。

回答

0

我發現真正的問題是什麼。
正如我已經嘗試獲取對象中的數組中的值。
對象本身是一個數組。所以

我怎樣努力是:

args.toString(); 

的對象不能被轉換爲字符串。

我由此實現:

args[0].toString(); //args[position].toString();  

因此如何我已經清除我的問題..