org.jivesoftware.smack.SmackException $ NoResponseException

时间:2016-02-10 10:57:44

标签: android xmpp

我尝试使用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();
        }   

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:-1)

我在我的项目中使用旧版本的Smack,但请参阅此内容,也许它会对您有所帮助。

在openfire中,我创建了一个用户名,用户名是userName,pass是密码。

下面的代码将抛出类似的例外:

    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();

希望它会对你有所帮助! :-)