org.jivesoftware.smack.SmackException$NoResponseException

org.jivesoftware.smack.SmackException$NoResponseException

我正在尝试使用 XMPP 服务器 (Openfire) 在我的 Android 应用程序中实现聊天(1 对 1 和群组),但是,在连接时我遇到以下异常 “org.jivesoftware.smack.SmackException$NoResponseException”在下一行

"connection.connect();"

我检查了 MyConstants.HOST、MyConstants.PORT、MyConstants.SERVICE 是否正确,但不知道为什么会出现此错误。

以下是我目前的代码,请检查并帮助我解决这个问题。

// Create a connection
        ConnectionConfiguration connConfig = new ConnectionConfiguration(MyConstants.HOST, MyConstants.PORT, MyConstants.SERVICE);
        connConfig.setDebuggerEnabled(true);
        connConfig.setSecurityMode(SecurityMode.disabled);

        connection = new XMPPTCPConnection(connConfig);

        try
        {
            connection.connect();
            Log.e("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+connection.getHost());

            if(connection.isConnected())
            {
                Map<String, String> attributes = new HashMap<String, String>();
                attributes.put("username", userId);
                attributes.put("password", MyConstants.PASSWORD);
                Registration reg = new Registration();
                reg.setType(IQ.Type.SET);
                reg.setTo(connection.getServiceName());
                reg.setAttributes(attributes);

                connection.sendPacket(reg);

                connection.login(userId, MyConstants.PASSWORD);
                Log.e("XMPPChatDemoActivity",  "Logged in as" + connection.getUser());
                //Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);                   
                Roster roster = connection.getRoster();
                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) 
                {
                    Log.d("XMPPChatDemoActivity",  "--------------------------------------");
                    Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                    Log.d("XMPPChatDemoActivity", "User: " + entry.getUser());
                    Log.d("XMPPChatDemoActivity", "Name: " + entry.getName());
                    Log.d("XMPPChatDemoActivity", "Status: " + entry.getStatus());
                    Log.d("XMPPChatDemoActivity", "Type: " + entry.getType());
                    Presence entryPresence = roster.getPresence(entry.getUser());
                    Log.d("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
                    Log.d("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());

                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                    Log.d("XMPPChatDemoActivity", "Presence : " + entryPresence);
                }
            }

        } 
        catch (XMPPException ex) 
        {
            Log.e("XMPPChatDemoActivity",  "[SettingsDialog] Failed to connect to "+ connection.getHost());
            Log.e("XMPPChatDemoActivity", ex.toString());
            connection = null;
        } 
        catch (SmackException e)
        {
            Log.e("Exception: ", e.toString());
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            Log.e("Exception: ", e.toString());
            e.printStackTrace();
        }   

我在我的项目中使用旧版本的 Smack,但请参考这个,也许它会对你有所帮助。

在 openfire 中,我创建了示例用户,登录名是用户名,密码是密码。

下面的代码将抛出与您类似的异常:

    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setServiceName(serviceName)
            .setHost(serverAddress)
            .setPort(serverPort)
            .setDebuggerEnabled(debugMode);

    connection = new XMPPTCPConnection(config.build());
    connection.connect();

结果:

  org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: AndFilter: (FromMatchesFilter (full): a0c88176-be28-4318-acb7-d08fd3803ef5@conference.makowka/jacek, StanzaTypeFilter:       org.jivesoftware.smack.packet.Presence).
  org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: AndFilter: (FromMatchesFilter (full): a0c88176-be28-4318-acb7-d08fd3803ef5@conference.makowka/jacek, StanzaTypeFilter: org.jivesoftware.smack.packet.Presence).

但改造后是这样的:

    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setServiceName(serviceName)
            .setHost(serverAddress)
            .setPort(serverPort)
            .setDebuggerEnabled(debugMode);

    config.setUsernameAndPassword(userName, password);
    connection = new XMPPTCPConnection(config.build());
    connection.connect().login();

一切正常!所以请尝试创建用户,在配置实例中设置这个用户并调用登录方法。那么解决的关键就在这里:

    connection.connect().login();

希望对您有所帮助! :-)

我以前遇到过同样的问题,确保你没有交换 MyConstants.HOST、MyConstants.PORT

这对我有用: Roster.setRosterLoadedAtLoginDefault(假);