【问题标题】:Connecting to a ejabberd XMPP server on localhost using Smack API on android在 android 上使用 Smack API 连接到本地主机上的 ejabberd XMPP 服务器
【发布时间】:2016-06-01 23:39:24
【问题描述】:

我在 Mac 上本地安装了一个 ejabberd XMPP 服务器。我正在使用此代码在 android 上使用 Smack API 进行连接和登录。

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("1@davids-macbook-pro.local", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("192.168.1.2")
                .setPort(Integer.parseInt("5222"))
                .build();

        AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
        try {
            conn2.connect();
            conn2.login();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

使用相同的用户名和密码,我可以使用任何其他 XMPP 客户端(如 Adium)登录,但上面的代码在 android 上会出现此错误 -

连接因错误而关闭 org.jivesoftware.smack.XMPPException$StreamErrorException: 主机未知

我的本​​地地址是 192.168.1.2,ejabberd 管理面板是 localhost:5280/admin

我阅读了文档并完成了所有编写的操作。代码有什么问题吗?

【问题讨论】:

    标签: android localhost xmpp ejabberd smack


    【解决方案1】:

    将 setHost() 替换为 setHostAddress()。我不知道为什么,但是 Windows 服务器上的 Ejabberd 为我提供了 TimeOut。如果您在 windows 上使用它,也可以使用基于 linux 的操作系统进行测试。

    【讨论】:

      【解决方案2】:

      此代码应该适用于 smack 4.2.4

      XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
                  .setHostAddress(InetAddress.getByName("192.168.1.2"))
                  .setUsernameAndPassword("1@davids-macbook-pro.local", "1")
                  .setXmppDomain(JidCreate.domainBareFrom("localhost"))
                  .setResource("Rooster")
                  .setKeystoreType(null) 
                  .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                  .setCompressionEnabled(true).build();
      

      【讨论】:

        【解决方案3】:

        您应该尝试以下操作:-

        1. 尝试将端口更改为 5280 或尝试 5227
        2. 检查您的服务器安全配置是必需的还是可选的,因为您已将安全设置为禁用
        3. 将连接监听器添加到您的 xmpp 连接
        4. setDebugEnable(true) 检查控制台中的 smack 日志并将日志添加到您的问题中,这样可以更清楚地了解究竟发生了什么
        5. 尝试将ConnectionConfiguration.SecurityMode.disabled 更改为XMPPTCPConnectionConfiguration.SecurityMode.requiredXMPPTCPConnectionConfiguration.SecurityMode.optional

        【讨论】:

          【解决方案4】:

          我可以通过这种方式连接和登录服务器 -

          config = XMPPTCPConnectionConfiguration.builder()
                          .setUsernameAndPassword("1", "1")
                          .setHost("192.168.1.2")
                          .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                          .setServiceName("davids-macbook-pro.local")
                          .setPort(Integer.parseInt("5222"))
                          .build();
          
                  AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
                  try {
                      conn2.connect();
                      conn2.login();
                  } catch (SmackException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (XMPPException e) {
                      e.printStackTrace();
                  }
          

          【讨论】:

            【解决方案5】:
            • 用户名后加“@”
            • setDebuggerEnabled(true)查看幕后发生的事情
            • 将 connect() 和 login() 方法移至后台线程
            • 使用连接监听器成功建立连接后调用 login()

            config = XMPPTCPConnectionConfiguration.builder()
                            .setUsernameAndPassword("1@davids-macbook-pro.local", "1")
                            .setHost("192.168.1.2")
                            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                            .setServiceName("davids-macbook-pro.local")
                            .setPort(5222)
                            .setDebuggerEnabled(true) // to view what's happening in detail
                            .build();
            
            AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
            try {
                conn2.connect(); // move it to a background thread instead of main thread
                // conn2.login();
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }
            
            
            conn2.addConnectionListener(new ConnectionListener() {
                @Override
                public void connected(XMPPConnection connection) {
                    new AsyncTask<Void, Void, Void>() {
                        @Override
                        protected Void doInBackground(Void... params) {
                            try {
                                conn2.login();
                            } catch (SmackException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (XMPPException e) {
                                e.printStackTrace();
                            }
                            return null;
                        }
                    }.execute();
                }
            
                @Override
                public void authenticated(XMPPConnection connection, boolean resumed) {
            
                }
            
                @Override
                public void connectionClosed() {
            
                }
            
                @Override
                public void connectionClosedOnError(Exception e) {
            
                }
            
                @Override
                public void reconnectionSuccessful() {
            
                }
            
                @Override
                public void reconnectingIn(int seconds) {
            
                }
            
                @Override
                public void reconnectionFailed(Exception e) {
            
                }
            });
            

            【讨论】:

              猜你喜欢
              • 2023-03-24
              • 2016-10-15
              • 2015-01-26
              • 1970-01-01
              • 1970-01-01
              • 2012-12-28
              • 2016-11-05
              • 2020-05-06
              • 1970-01-01
              相关资源
              最近更新 更多