专业IM即时通讯软件开发,值得信赖!

smack:A yielded an error response NX_DOMAIN AAAA yielded an error response NX_DOMAIN

XMPP 云聊IM 126℃
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!");
}

参考文档:

喜欢 (0)
仿微信聊天软件开发
点击这里给我发消息