【问题标题】:Error while trying to connect openfire using smack on android尝试在 android 上使用 smack 连接 openfire 时出错
【发布时间】:2016-07-04 15:39:40
【问题描述】:
XMPPTCPConnectionConfiguration.Builder configBuilder =  XMPPTCPConnectionConfiguration.builder();
    configBuilder.setUsernameAndPassword("test", "test");
    configBuilder.setResource("test");
    configBuilder.setServiceName("37.139.26.142");
    configBuilder.setHost("37.139.26.142");
    configBuilder.setPort(5222);
    configBuilder.setSendPresence(true);
    configBuilder.setDebuggerEnabled(true);
    configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.required );
    SASLMechanism mechanism = new SASLDigestMD5Mechanism();
    SASLAuthentication.registerSASLMechanism(mechanism);
    SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
    SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5");
    AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
    try {
        connection.connect();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    try {
        connection.login();
    } catch (XMPPException e) {
        e.printStackTrace();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我目前正在尝试握手我的 android 应用程序和我的 openfire 服务器(在 ubuntu 上工作)。但我做不到。我没有任何失败或什么的。只是什么都没有发生。这感觉很糟糕。

【问题讨论】:

    标签: android chat openfire smack


    【解决方案1】:

    您是否尝试发送消息?你确定你没有连接? 您是否检查过 Openfire 管理员您的测试用户未连接?

    首先我建议你尝试发送消息:

    ChatManager chatmanager = ChatManager.getInstanceFor(connection);
    Chat newChat = chatmanager.createChat("anotheruser@yourdomain", new MessageListener() {
        public void processMessage(Chat chat, Message message) {
            System.out.println("Received message: " + message);
        }
    });
    
    try {
        newChat.sendMessage("Howdy!");
    }
    catch (XMPPException e) {
        System.out.println("Error Delivering block");
    }
    

    我的代码来自:http://www.igniterealtime.org/builds/smack/docs/latest/documentation/messaging.html

    另一个建议是禁用安全模式,只是为了测试。

    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    

    如果这些都不起作用,请尝试使用下面的配置,这对我有用。

    XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
    
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    config.setServiceName(serverAddress);
    config.setHost(serverAddress);
    config.setPort(5222);
    config.setDebuggerEnabled(true);
    connection = new XMPPTCPConnection(config.build());
    
    try {
        connection.connect();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    try {
        connection.login(loginUser, passwordUser);
    } catch (XMPPException e) {
        e.printStackTrace();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 我从晚上开始就尝试了你的说法和其他一些东西。现在我相信这与 android 部分无关。这是关于 Openfire 部分。我在 Android Logcat 看到了这个; W/System.err: org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '37.139.26.142:5222' failed because java.net.SocketTimeoutException: failed to connect to /37.139.26.142 (port 5222) after 30000ms 我检查了我的服务器here但它失败了。 (我提供这些 IP 地址是因为它没有任何东西。这只是我为培训而构建的测试服务器。)
    • 我将主机操作系统从 Ubuntu 更改为 Centos。现在我可以从 Spark 客户端访问。但仍然无法通过Android访问。我现在收到org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 错误。
    • 你是否为任何 ip 打开你的 5222 端口?你是这样使用这个配置的吗? config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    • 我解决了。是的,我像你说的那样使用它。您可以检查第一篇文章的代码。谢谢你的帮助。
    • 不客气。我在 AWS 的 Ubuntu 14 上运行我的服务器,它工作正常
    【解决方案2】:

    首先我发现这不是关于 Android 部分,而是关于 Openfire 部分。因为我无法将它与 Spark 连接,而我在 Logcat 中看到了这个;

    W/System.err: org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '37.139.26.142:5222' failed because java.net.SocketTimeoutException: failed to connect to /37.139.26.142 (port 5222) after 30000ms
    

    然后我做了一些研究并尝试了一些东西,我发现它是关于 Ubuntu(至少对我而言)。然后我把我的 openfire 服务器搬到了 Centos。然后我就可以用 Spark 连接到它了。然后我又遇到了一个问题。

    org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
    

    我用下面的代码解决了这个问题。我希望这可以帮助其他人。

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);    
    
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
            SmackConfiguration.DEBUG = true;
            XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
            configBuilder.setUsernameAndPassword("test", "test");
            configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            configBuilder.setResource("test");
            configBuilder.setServiceName("enter your server ip here");
            configBuilder.setHost("eneter your server ip here");
            configBuilder.setPort(5222);
            configBuilder.setSendPresence(true);
            configBuilder.setDebuggerEnabled(true);
            SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
            SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
            SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
    
            XMPPTCPConnection connection;
            connection = new XMPPTCPConnection(configBuilder.build());
            // Connect to the server
            try {
                connection.connect();
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }
            // Log into the server
    
            try {
                connection.login();
            } catch (XMPPException e) {
                e.printStackTrace();
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 2013-03-04
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多