无法连接到本地Openfire服务器(使用smack api)

时间:2015-05-15 23:35:29

标签: android xmpp smack

我在Android上尝试使用此代码连接到我的本地XMPP服务器

Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    "192.168.43.170", 5223, "192.168.43.170");
            XMPPConnection connection = new XMPPConnection(connConfig);

            try {
                connection.connect();
            } catch (XMPPException ex) {
            }
            try {
                // SASLAuthentication.supportSASLMechanism("PLAIN", 0);
                connection.login("nagham","123456");
                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                setConnection(connection);
                Roster roster = connection.getRoster();
                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) {
                    Presence entryPresence = roster.getPresence(entry
                            .getUser());

                    Presence.Type type = entryPresence.getType();
                }
            } catch (XMPPException ex) {    
        }
        }
    });

但我得到以下例外:

    05-16 02:10:16.580: E/XMPPChatDemoActivity(24289): Failed to connect to 192.168.43.170
05-16 02:10:16.588: E/XMPPChatDemoActivity(24289): XMPPError connecting to 192.168.43.170:5223.: remote-server-error(502) XMPPError connecting to 192.168.43.170:5223.
05-16 02:10:16.588: E/XMPPChatDemoActivity(24289):   -- caused by: java.net.ConnectException: failed to connect to /192.168.43.170 (port 5223): connect failed: ETIMEDOUT (Connection timed out)
05-16 02:10:16.588: E/AndroidRuntime(24289): FATAL EXCEPTION: Thread-5736
05-16 02:10:16.588: E/AndroidRuntime(24289): java.lang.IllegalStateException: Not connected to server.

我哪里错了? 请注意,我使用的是asmack-2010.05.07

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题,只是将端口号从5223更改为5222 并且每个人都不会忘记必须禁用防火墙。