2016-07-25 61 views
0

我需要连接到socket并发送凭据信息才能通过接收即将发生的数据RECEIVE_CRISIS在Android中通过套接字发送数据

我正在使用库适用于Android的Socket.IO。由于我没有从连接中获得日志,所以调试真的很难,我不知道为什么以及它在哪里失败。自从我开始在Android上工作以来,我从未从服务器收到任何内容。 Android上的NSDictionary是否等于JSONObject?在Android上,sendEvent等于send()还是emit()?我是否需要将JSON作为对象或数组发送?最后,如何获取日志错误?

它的工作iOS上的一部分,所以我有点失去..

这是的iOS代码:(我修改地址为安全目的):

NSDictionary *[email protected]{@"user":_username,@"orgid":_organizationId}; 
[_socketIO connectToHost:@"nodejs.myserver.com" onPort:3000 ]; 
    [_socketIO sendEvent:@"joinparty" withData:params]; 

这是安卓代码:

private void connectAndListen(int username) throws Exception { 
    socket = IO.socket("nodejs.myserver.com:3000"); 
    socket.connect(); 

    JSONObject json = new JSONObject(); 
    json.put("user", username); 
    json.put("orgid", settings.getString("orgid", "")); 

    Log.e(TAG, json.toString()); 

    socket.send("joinparty",json); 
    socket.emit("joinparty", json); 

    socket.on(RECEIVE_CRISIS, onCrisis); 
} 

修订问题

private void connectAndListen(int id) throws Exception { 
    IO.setDefaultSSLContext(SSLContext.getDefault()); 

    // Set default hostname 
    HostnameVerifier hostnameVerifier = new HostnameVerifier() { 
     @Override 
     public boolean verify(String hostname, SSLSession session) { 
      HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); 
      return true; 
     } 
    }; 
    IO.setDefaultHostnameVerifier(hostnameVerifier); 
    IO.Options options = new IO.Options(); 
    // set as an option 
    options.sslContext = SSLContext.getDefault(); 
    options.hostnameVerifier = hostnameVerifier; 
    options.secure = true; 
    socket = IO.socket("nodejs.myserver.com:3000", options); 
    socket.connect(); 
    JSONObject json = new JSONObject(); 
    json.put("user", id); 
    json.put("orgid", settings.getString("orgid", "")); 

    Map<Object, Object> arrays = new HashMap<>(); 
    arrays.put("user", id); 
    arrays.put("orgid",settings.getString("orgid", "")); 

    socket.emit("joinparty", arrays); 
    socket.on(RECEIVE_CRISIS, onCallback); 
    socket.on("connect_error", onCallback); 
    socket.on("connect", onCallback); 
    socket.on("Object", onCallback); 
    socket.on("connect_timeout", onCallback); 
} 

private Emitter.Listener onCallback = new Emitter.Listener() { 
    @Override 
    public void call(Object... args) { 
     Log.e(TAG, args[0]+""); 
    } 
}; 

回答

1

Is the NSDictionary is equivalent to the JSONObject on Android?

不,不是这样的。 NSDictionary是class cluster,这意味着实际的实现对你来说是API用户隐藏的。事实上,Foundation框架会在运行时选择合适的实现基于数据等最接近它的Java量Map<Object, Object>

Is sendEvent equivalent to send() or emit() on Android?

它发出的Android。 from official documentation

DO I need to send the JSON as an Object or an Array?

尝试发送Map来代替。 JSON是另一回事。如果您的服务器期望使用JSON格式的数据,则只需发送它。

Finally, How to I get the log error?

您需要为它使用combination of emitter and manager

+0

现在我终于看到一个错误:xhr poll poll error。我认为这可能与SSL认证有关,因为我使用Retrofit这样的问题。我更新了我的问题 – Jaythaking

+0

对于不同的错误,请发布其他问题并提供必要的详细信息。 –

+0

虽然最初的问题并未解决 – Jaythaking