尝试在Android上使用smack连接openfire时出错

时间:2016-03-18 02:15:04

标签: android chat openfire smack

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上工作)。但我不能。我没有任何失败或什么。什么都没发生。那感觉很糟糕。

2 个答案:

答案 0 :(得分:7)

您是否尝试发送消息?你确定没有连接吗? 您是否检查过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

另一个建议是禁用SecurityMode,仅用于测试。

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();
        }

答案 1 :(得分:0)

首先,我发现这与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.

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

公共类MainActivity扩展了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();
    }

}

}