smack 4.1.0 beta3 上的 xmppconnection 错误

xmppconnection error on smack 4.1.0 beta3

我已经在本地主机上测试了带有 Smack 4.1.0 Beta3 的 eclipse 和 openfire 之间的连接,但是出现了一些错误。

XMPPTCPConnectionConfiguration conf =XMPPTCPConnectionConfiguration.builder()
            .setHost("127.0.0.1")
            .setPort(5222)
            .setCompressionEnabled(false)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .build();
    XMPPTCPConnection connection = new XMPPTCPConnection(conf);

    try {
        connection.connect();
        connection.login("admin","admin");
    } catch (SmackException | IOException | XMPPException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

当我 运行 在 android 申请时 "Emulator" 我收到消息 Unfortunately,test_connection has stopped. 和 LogCat 显示

FATAL EXCEPTION: main 
Process:com.example.test_connection, PID: 1956
java.lang.NoClassDefFoundError: org.jxmpp.util.XmppStringUtils

您没有写明您使用的是什么版本的openfire。如果是 3.9.3,下面的示例应该会有所帮助。祝你好运!

Smack 版本: smack-4.2.0-alpha1-SNAPSHOT Openfire 版本:3.9.3

Smack 库: 核心,im,java7,sasl 提供,tcp

有额外的库: jxmpp-util-cache-0.4.1.jar, jxmpp-core-0.4.1.jar, minidns-0.1.1.jar

请按照下面的代码:

    public class MyConnection implements ConnectionListener {

    ...

    XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    config.setUsernameAndPassword(userName + "@" + DOMAIN, password);
    config.setServiceName(DOMAIN);
    config.setHost(DOMAIN);
    config.setPort(PORT);
    config.setDebuggerEnabled(true);

    AbstractXMPPConnection mConnection = new XMPPTCPConnection(config.build());
    mConnection.setPacketReplyTimeout(10000);
    mConnection.addConnectionListener(this);

    try {
        mConnection.connect();
    } catch (SmackException | IOException | XMPPException e) {
        e.printStackTrace();
    }


    @Override
    public void connected(XMPPConnection xmppConnection) {
         Log.d(TAG, "connected");

         try {
              mConnection.login();
         } catch (XMPPException | SmackException | IOException e) {
              e.printStackTrace();
         }
    }

您必须覆盖的其他方法:

@Override
public void authenticated(XMPPConnection xmppConnection, boolean b) {
    Log.d(TAG, "authenticated");
}

@Override
public void connectionClosed() {
    Log.d(TAG, "connectionClosed");
}

@Override
public void connectionClosedOnError(Exception e) {
    Log.d(TAG, "connectionClosedOnError" + e.toString());
}

@Override
public void reconnectionSuccessful() {
    Log.d(TAG, "reconnectionSuccessful");
}

@Override
public void reconnectingIn(int i) {
    Log.d(TAG, "reconnectingIn");
}

@Override
public void reconnectionFailed(Exception e) {
    Log.d(TAG, "reconnectionFailed");
}