XMPPTCPConnectionConfiguration config = null;
try {
config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("admin", "thepass")
.setXmppDomain("192.168.1.3")
.setHost("192.168.209.2")
.setPort(5222)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
} catch (Exception e) {
e.printStackTrace();
}
AbstractXMPPConnection conn1 = new XMPPTCPConnection(config);
conn1.setReplyTimeout(60000);
conn1.setPacketReplyTimeout(60000);
conn1.connect();
今天发现,上面的代码,在有些Android手机上会报如下错误
The following addresses failed: '192.168.209.2:5222' failed because: org.minidns.hla.ResolutionUnsuccessfulException: Asking for 192.168.209.2. IN A yielded an error response NX_DOMAIN, '192.168.209.2:5222' failed because: org.minidns.hla.ResolutionUnsuccessfulException: Asking for 192.168.209.2. IN AAAA yielded an error response NX_DOMAIN
原因如下:
在以前版本的smack中,connectionconfiguration.set host(string)可用于设置xmpp服务的主机IP地址。由于添加了DNSSEC支持,这不再可能。您必须使用新的连接配置connection configuration.sethostaddress(inetaddress)。
所以代码修改如下:
InetAddress addr = InetAddress.getByName("192.168.209.2");
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return false;
}
};
DomainBareJid serviceName = JidCreate.domainBareFrom("localhost");
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setHost(server) # it will be resolved by setHostAddress method
.setUsernameAndPassword("davood","mypass")
.setPort(5222)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setXmppDomain(serviceName)
.setHostnameVerifier(verifier)
.setHostAddress(addr)
.setDebuggerEnabled(true)
.build();
AbstractXMPPConnection conn1 = new XMPPTCPConnection(config);
conn1.connect();
if(conn1.isConnected()){
Log.d("XMPP","Connected");
}
conn1.login();
if(conn1.isAuthenticated()){
Log.d("XMPP","Authenticated");
EntityBareJid jid = JidCreate.entityBareFrom("sadegh@localhost");
org.jivesoftware.smack.chat2.Chat chat = ChatManager.getInstanceFor(conn1).chatWith(jid);
chat.send("Eureka, I am connected!");
}
参考文档:
- https://github.com/igniterealtime/Smack/wiki/Smack-4.2-Readme-and-Upgrade-Guide
- failed because: de.measite.minidns.hla.ResolutionUnsuccessfulException: Asking for xxxx. IN AAAA yielded an error response NX_DOMAIN
- https://baike.baidu.com/item/DNSSec
版权声明:部分文章、图片等内容为用户发布或互联网整理而来,仅供学习参考。如有侵犯您的版权,请联系我们,将立刻删除。