我测试了eclipse与Smack 4.1.0 Beta3之间的连接以及localhost中的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应用程序上运行"模拟器"
我收到了消息Unfortunately,test_connection has stopped.
和LogCat显示
FATAL EXCEPTION: main
Process:com.example.test_connection, PID: 1956
java.lang.NoClassDefFoundError: org.jxmpp.util.XmppStringUtils
答案 0 :(得分:0)
你没有写过你正在使用的openfire版本。如果它3.9.3下面的例子应该是有帮助的。祝你好运!
Smack版本: smack-4.2.0-alpha1-SNAPSHOT Openfire版本: 3.9.3
Smack libs: core,im,java7,sasl-provided,tcp
使用其他库: jxmpp-UTIL缓存-0.4.1.jar, jxmpp核-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");
}