【问题标题】:Connect to local openfire server android连接到本地openfire服务器android
【发布时间】:2015-03-13 18:07:52
【问题描述】:

我正在 Ubuntu 上开发。我正在尝试将我的 xmpp 客户端连接到本地 openfire 服务器。

AndroidConnectionConfiguration configuration = new AndroidConnectionConfiguration(
                host, Integer.parseInt(port), service);
        SASLAuthentication.supportSASLMechanism("PLAIN", 0); // (I tried after removing this line)
        configuration.setSASLAuthenticationEnabled(true); 
        configuration.setDebuggerEnabled(true);
        XMPPConnection connection = new XMPPConnection(configuration);

        try {
            connection.connect();
            Log.i("XMPPClient",
                    "[SettingsDialog] Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to "
                    + connection.getHost());
            Log.e("XMPPClient", ex.toString());
            xmppClient.setConnection(null);
        }
        try {
            connection.login(username, password);
            Log.i("XMPPClient", "Logged in as " + connection.getUser());

            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            xmppClient.setConnection(connection);
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as "
                    + username);
            Log.e("XMPPClient", ex.toString());
            xmppClient.setConnection(null);
        }

现在我可以使用此代码连接 Google 聊天服务器。我也可以在 Ubuntu 上将本地服务器与 Spark 客户端连接。但无法连接到 Android 上的本地服务器。

其中 host 为 10.0.2.2 (Android localhost) 端口 5222 服务 本地主机

Android客户端可以成功连接本地服务器但我无法登录

我收到的错误sasl authentication failed using mechanism digest-md5

我正在搜索过去两天并尝试了很多东西但本地服务器没有成功。

【问题讨论】:

    标签: android xmpp openfire asmack


    【解决方案1】:

    试试这个连接:

    public static boolean XMPPConnect() {
            try {
                System.setProperty("java.net.preferIPv6Addresses", "false");
                //SmackConfiguration.setPacketReplyTimeout(30000);
    
                config = new ConnectionConfiguration(Constant._hostName, port);
                //config = new AndroidConnectionConfiguration(Constant._hostName);
    
                //          config.setCompressionEnabled(true);
                //          config.setSASLAuthenticationEnabled(true);
                //          SmackConfiguration.setPacketReplyTimeout(1000*60);
                //          config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
                //          config.setReconnectionAllowed(true);
                //          //config.setCompressionEnabled(true);
                config.setRosterLoadedAtLogin(true);
                config.setSendPresence(true);
                //SASLAuthentication.supportSASLMechanism("MD5");
                config.setSASLAuthenticationEnabled(true);
                config.setCompressionEnabled(true);
                config.setSecurityMode(SecurityMode.enabled);
                config.setReconnectionAllowed(true);
    
                SmackConfiguration.setPacketReplyTimeout(30000);
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    config.setTruststoreType("AndroidCAStore");
                    config.setTruststorePassword(null);
                    config.setTruststorePath(null);
                }/*else if (Build.VERSION.SDK_INT<19 || Build.VERSION.SDK_INT>= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
    
                }*/ else {
                    config.setTruststoreType("BKS");
                    String path = System.getProperty("javax.net.ssl.trustStore");
                    if (path == null)
                        path = System.getProperty("java.home") + File.separator + "etc"
                                + File.separator + "security" + File.separator
                                + "cacerts.bks";
                    config.setTruststorePath(path);
                }
    
                connection = new XMPPConnection(config);
                config.setSASLAuthenticationEnabled(true);
                connection.connect();
                debugEnabledReset();
    
    
    
    
            } catch (Exception e) {
                XMPPConstants.XMPP_ERROR="socket_timeout";
                e.printStackTrace();
                if(connection.DEBUG_ENABLED==true)
                    connection.DEBUG_ENABLED = false;
                return false;
            }
            return true;
        }
    

    如果它返回true那么你登录到xmpp如下:

    public static boolean XMPPLogin(String uname, String password) {
    
            Roster roster = connection.getRoster();
            roster.addRosterListener(new RosterListener() {
                public void presenceChanged(Presence arg0) {}
                public void entriesUpdated(Collection<String> arg0) {}
                public void entriesDeleted(Collection<String> arg0) {}
                public void entriesAdded(Collection<String> arg0) {}
            });
    
            try {
    
    
                //SASLAuthentication.supportSASLMechanism("MD5", 0);
                connection.login(uname, password, "Smack");
    
    
            } catch (Exception e) {
                XMPPConstants.XMPP_ERROR="Username or password is incorrect";
                if(e.getMessage().toString().contains("No response")){
                    XMPPConstants.XMPP_ERROR="Server communication failed";
                }
                e.printStackTrace();
                if(connection.DEBUG_ENABLED==true)
                    connection.DEBUG_ENABLED = false;
                return false;
            }
            return true;
        }
    

    【讨论】:

    • 去掉这一行 config.setSASLAuthenticationEnabled(true);
    • 仍然出现错误Failed to log in as root SASL authentication failed using mechanism DIGEST-MD5:
    • 对您的本地 Openfire 服务器设置执行更改,例如在服务器上启用 SASL 身份验证。
    • 您能告诉我在哪里可以启用 SASL 吗?服务器上启用了 SSL。
    • 在你的本地服务器上配置你需要启用的 openfire。
    猜你喜欢
    • 2015-09-19
    • 2023-03-26
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多