Openfire下载地址http://www.igniterealtime.org/downloads/index.jsp
本文Openfire服务器名称为gwcheng-pc
Smack 4.1.4的官方文档下载地址包括jar包和javadoc(官方给的解释Easy to use Java XMPP client library.)
http://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_4_1_4.zip
下载Smack之后从文档smack_4_1_4/releasedocs/documentation/index.html给的示例
// Create a connection to the gwcheng-pc server on a specific port.
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("cheng", "cheng")
.setServiceName("gwcheng-pc")
.setHost("gwcheng-pc")
.build();
AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
conn2.connect();
建立一个java工程,拷贝smack_4_1_4\libs\下的jar包到自己的工程里。
运行上面的代码(代码中”cheng”是我在openfire中注册的用户名和密码”gwcheng-pc”为我的openfire服务器名称 )
运行上面的程序,会报如下的错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory
这个错误很明显是缺少jar包 xmlpull.jar,此处导入xmlpull-1.1.3.1.jar
导入之后运行还是会报错
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalStateException: org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available
Caused by: org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available
这种错误还是缺少jar包 kxml.jar,此处导入kxml2-2.3.0.jar
导入之后运行还是会报错
Exception in thread "main" java.lang.NoClassDefFoundError: de/measite/minidns/DNSCache
Caused by: java.lang.ClassNotFoundException: de.measite.minidns.DNSCache
继续导入jar包 minidns.jar,此处导入minidns-0.1.7.jar
导入之后运行还是会报错
Exception in thread "main" java.lang.NoClassDefFoundError: org/jxmpp/util/cache/ExpirationCache
Caused by: java.lang.ClassNotFoundException: org.jxmpp.util.cache.ExpirationCache
继续导入jar包 jxmpp-core-0.5.0-alpha6.jar和 jxmpp-util-cache-0.4.0-alpha1.jar
导入之后运行还是会报错
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/StrictHostnameVerifier
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.ssl.StrictHostnameVerifier
继续导入jar包 httpclient-4.4.1.jar
导入之后运行还是会报错
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
继续导入jar包 commons-logging-1.2.jar
导入之后运行还是会报错
org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
这个错误最严重,到目前我也没有找到合适的解决方法,试过了添加本地证书什么的都不行
无赖之下只有改变链接方式
AbstractXMPPConnection connection = null;
Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setSecurityMode(SecurityMode.disabled);
XMPPTCPConnectionConfiguration config = builder
.setServiceName("gwcheng-pc")
.setHost("gwcheng-pc").setPort(5222)
.build();
connection = new XMPPTCPConnection(config);
connection.connect();
connection.login("cheng", "cheng");
只有这样才能成功连接到openfire服务器,并登录,上述代码中的”gwcheng-pc”是openfire服务器名称,”cheng”是我在openfire上创建的用户的用户名和密码,都为”cheng”。
conn.connect();
conn.login("cheng", "cheng");
// 发送消息
//Assume we've created an XMPPConnection name "conn"
ChatManager chatmanager =ChatManager.getInstanceFor(conn);
Chat newChat = chatmanager.createChat("man@gwcheng-pc");
newChat.sendMessage("我是程序员");
其中”man@gwcheng-pc”是openfire服务器中注册的另一个用户的jid
发送消息官方文档说明
The Chat.sendMessage(String) method is a convenience method that creates a Message object, sets the body using the String parameter, then sends the message.
try {
AbstractXMPPConnection conn = GetXMPPConnection.getConnection();
conn.connect();
conn.login("man", "man");
ChatManager chatmanager = ChatManager.getInstanceFor(conn);
System.out.println("等待接受消息...");
chatmanager.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean create) {
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message msg) {
if (null != msg.getBody()) {
System.out.println("接收到新消息:" + msg.getBody());
}
}
});
}
});
} catch (XMPPException e) {
e.printStackTrace();
}
while (true);
完整示例代码下载地址
https://github.com/peer44/smark4
http://www.oschina.net/code/snippet_2313055_48750