代码之家  ›  专栏  ›  技术社区  ›  Leo Wiki

org.jivesoftware.smack.SmackException$NoResponseException

  •  1
  • Leo Wiki  · 技术社区  · 10 年前

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

    " connection.connect(); "

    我检查了MyConstants。主持人,MyConstants。端口,MyConstants。服务是正确的,但不知道为什么我会得到这个错误。

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

    // 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 回复  |  直到 10 年前
        1
  •  1
  •   Dnyanesh M    9 年前

    我以前也遇到过同样的问题,请确保您没有交换MyConstants。主机,MyConstants.PORT

        2
  •  1
  •   Mitch    9 年前

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

        3
  •  -1
  •   Mack94    10 年前

    我在我的项目中使用了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();
    

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