Smack 4.2.0 报错:IN AAAA 返回 NX_DOMAIN 错误响应。

11

我开始使用OpenFire并通过Spark进行测试,一切正常,但是当我尝试在Android Studio中使用Smack 4.2.0连接时,我收到以下错误:

Ljavax/naming/directory/InitialDirContext;

我的依赖关系如下:

compile "org.igniterealtime.smack:smack-java7:4.2.0" compile "org.igniterealtime.smack:smack-tcp:4.2.0" compile "org.igniterealtime.smack:smack-im:4.2.0" compile "org.igniterealtime.smack:smack-extensions:4.2.0" compile "org.igniterealtime.smack:smack-android-extensions:4.2.0" compile "org.igniterealtime.smack:smack-bosh:4.2.0"

当我从依赖关系中删除以下内容: "compile org.igniterealtime.smack:smack-java7:4.2.0" 并添加以下内容时: compile "org.igniterealtime.smack:smack-android:4.2.0" 我的依赖关系变成这样:

compile 'com.android.support:appcompat-v7:24.0.0' compile "org.igniterealtime.smack:smack-android:4.2.0" compile "org.igniterealtime.smack:smack-tcp:4.2.0" compile "org.igniterealtime.smack:smack-im:4.2.0" compile "org.igniterealtime.smack:smack-extensions:4.2.0" compile "org.igniterealtime.smack:smack-android-extensions:4.2.0" compile "org.igniterealtime.smack:smack-bosh:4.2.0"

我收到以下错误:

org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '192.168.209.2:5222' failed because: de.measite.minidns.hla.ResolutionUnsuccessfulException: Asking for 192.168.209.2. IN A yielded an error response NX_DOMAIN, '192.168.209.2:5222' failed because: de.measite.minidns.hla.ResolutionUnsuccessfulException: Asking for 192.168.209.2. IN AAAA yielded an error response NX_DOMAIN

当我尝试使用conn.connect()时,造成错误的代码部分如下:

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();  

在登录到XMPP服务器时,删除.setXmppDomain("192.168.1.3")方法和setServiceName(),并提供用户名和密码。 - Rakesh Kalashetti
这在Smack 4.1.6中有效,我目前正在使用它,对我很有帮助,谢谢。 - Rakesh Kalashetti
是的,它可以在4.2.0以上的版本中工作。目前我正在使用4.1.9并且它能够正常运行,但我需要更新到新版本的4.2.0。 - sadegh
请使用.setHostAddress(InetAddress.getByName(DOMAIN_IP_ADDRESS))代替.setHost(HOST)。 - Shubham AgaRwal
3个回答

22
你遇到的错误源于未完全指定XMPP服务器的地址。
想象这种情况:
我的ejabberd服务器运行在此地址上:192.168.209.2#ejabberd服务器
有一个名为“localhost”的xmpp域,有两个JID,“davood@localhost”和“sadegh@localhost”。
在Smack中,我想要使用我的用户"davood@localhost"进行身份验证。然后我按照以下步骤执行:
            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!");


            }

我也遇到了连接模拟器到ejabberd服务器的同样问题。我按照你的代码片段将主机地址设置为'InetAddress addr = InetAddress.getByName("10.0.2.2")',但它抛出了这个异常:以下地址失败:'null:5222'。 - Nasser Tahani
伊朗的开发者总是最好的。 - Mohsen Emami

9
请检查: https://github.com/igniterealtime/Smack/wiki/Smack-4.2-Readme-and-Upgrade-Guide 在 Smack 的之前版本中,可以使用 ConnectionConfiguration.setHost(String) 来设置 XMPP 服务的主机 IP 地址。由于增加了 DNSSEC 支持,这已不再可能。现在必须使用新的连接配置 ConnectionConfiguration.setHostAddress(InetAddress)。
您也可以查看以下链接: failed because: de.measite.minidns.hla.ResolutionUnsuccessfulException: Asking for xxxx. IN AAAA yielded an error response NX_DOMAIN

0

我在Kotlin中遇到了与Xmpp连接相关的问题,根据@davood-falahati的答案并进行了一些更改,我实现了解决方案,对我来说效果很好:

private fun initializeXmppConnection(){
        val addr: InetAddress = InetAddress.getByName("90.182.109.19")
        val verifier: HostnameVerifier = HostnameVerifier { s, sslSession -> false }
        val serviceName: DomainBareJid = JidCreate.domainBareFrom("im.mydomain.ir")
        val config: XMPPTCPConnectionConfiguration = XMPPTCPConnectionConfiguration.builder()
            .setHost("90.182.109.19")
            .setUsernameAndPassword("60b9d4d75943a","9yqx7heu6aok4g40so8s0w8oocow8w8")
            .setPort(80)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setXmppDomain(serviceName)
            .setHostnameVerifier(verifier)
            .setHostAddress(addr)
            .setSendPresence(true)
            .setCompressionEnabled(false)
            .setConnectTimeout(30_000)
            .build()

        val conn: AbstractXMPPConnection = XMPPTCPConnection(config)
        conn.connect();

        if(conn.isConnected())
            Log.d("msn","Connected");

        conn.login();

        if(conn1.isAuthenticated()){
            Log.d("msn","Authenticated");

        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接