我正在尝试使用XMPP服务器(Openfire)在我的Android应用程序中实现聊天(一对一和群组聊天),但是,在连接时出现以下异常
下一行中的“ 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();
        }

最佳答案

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

关于android - org.jivesoftware.smack.SmackException $ NoResponseException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35313282/

10-11 17:17