尝试在Android上使用aSmack连接到XMPP服务器让我“没有dns解析器处于活动状态”

时间:2013-06-06 13:10:14

标签: android dns xmpp smack

我在代码中尽可能简单。我在版本8-0.8.3中使用asmack库for android。

我的代码:

package info.zajacmp3.servercommunication;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class XmppService extends Service{



    public void xmppService() throws XMPPException {

        Connection conn1 = new XMPPConnection("jabber.org");
        conn1.connect();
    }


    @Override
    public void onCreate(){
        //TODO:actions to perform when service is created
        try {
            xmppService();
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Replace with service binding
        return null;
    }
}

它冻结我的应用程序并导致我和错误:没有dns解析器激活。网上没有任何关于它的内容。

我真的希望得到一些帮助或线索。

也尝试过这样:

private final static String server_host =“jabber.org”; private final static int SERVER_PORT = 5222;

public void xmppService()抛出XMPPException {

ConnectionConfiguration config = new ConnectionConfiguration( server_host, SERVER_PORT);
XMPPConnection m_connection = new XMPPConnection(config);
        try {
             SASLAuthentication.supportSASLMechanism("PLAIN");
             config.setSASLAuthenticationEnabled(true);     
             m_connection.connect();
            Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
        } catch (XMPPException e) {
            e.printStackTrace();
        } 

}

@UPDATE:

使用smack库而不是asmack让我遇到同样的问题。 我没有得到错误日志,但在断开调试器后我得到了:

enter image description here

3 个答案:

答案 0 :(得分:6)

如果aSmack告诉您没有DNS解析器处于活动状态,那么您很可能没有初始化aSmack的静态代码,因为aSmack's README告诉您这样做。

来自自述文件

  

为了在Android上正常工作,您需要注册Smack   手动提供XMPP Providers和Extensions并初始化一些静态代码   在执行任何XMPP激活之前阻塞。调用   SmackAndroid.init(Context)(在org.jivesoftware.smack中)将执行此操作   对你而言。

只需致电SmackAndroid.init(Context)即可。

答案 1 :(得分:2)

我建议使用Smack而不是aSmack。 Asmack是Smack的补丁和增强版本,aSmack有点死了。在Smack中完成了代码重构,添加了一些新方法和重构的DNS类。

请参阅此Smack

<强>更新
看到您的错误日志后,似乎是主UI线程上的网络调用

的问题

如何解决NetworkOnMainThread异常?
使用AsyncTask可以在不同的线程(后台)而不是应用程序的主线程上调用您的网络。

将代码移至AsynTask: -

private class ConnectToXmpp extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
          ConnectionConfiguration config = new ConnectionConfiguration( server_host, SERVER_PORT);
          XMPPConnection m_connection = new XMPPConnection(config);
    try {
         SASLAuthentication.supportSASLMechanism("PLAIN");
         config.setSASLAuthenticationEnabled(true);     
         m_connection.connect();
        Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
    } catch (XMPPException e) {
        e.printStackTrace();
    } 

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

    }

}

现在你可以执行你的AsyncTask了: -

new ConnectToXmpp().execute();

这样您的网络通话将在后台以不同的线程进行。

请参阅 AsyncTask

答案 2 :(得分:1)

这对我有用:

            int portInt = 5222;
            String host = "192.168.0.104";
            String service = "something.local" //Domain name
            String username = "username" //Without domain name
            String password = "password"

            // Create a connection
            ConnectionConfiguration connConfig = new ConnectionConfiguration(host, portInt,service);
            connConfig.setSASLAuthenticationEnabled(true);
            //connConfig.setCompressionEnabled(true);
            connConfig.setSecurityMode(SecurityMode.enabled);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                connConfig.setTruststoreType("AndroidCAStore");
                connConfig.setTruststorePassword(null);
                connConfig.setTruststorePath(null);
                Log.i("XMPP", "Build Icecream");

            } else {
                connConfig.setTruststoreType("BKS");
                String path = System.getProperty("javax.net.ssl.trustStore");
                if (path == null)
                    path = System.getProperty("java.home") + File.separator + "etc"
                            + File.separator + "security" + File.separator
                            + "cacerts.bks";
                connConfig.setTruststorePath(path);
                Log.i("XMPP", "Build less than Icecream ");

            }
            connConfig.setDebuggerEnabled(true);
            XMPPConnection.DEBUG_ENABLED = true;
            XMPPConnection connection = new XMPPConnection(connConfig);

            try {
                connection.connect();
                Log.i("XMPP", "Connected to " + connection.getHost());
                // publishProgress("Connected to host " + HOST);
            } catch (XMPPException ex) {
                Log.e("XMPP", "Failed to connect to " + connection.getHost());
                Log.e("XMPP", ex.toString());
                //publishProgress("Failed to connect to " + HOST);
                //xmppClient.setConnection(null);
            }

            try {
                connection.login(username, password);
                Log.i("androxmpp", "Logged in " + connection.getUser() + ". Authenticated : "+connection.isAuthenticated());
            } catch(Exception ex){

                Log.i("androxmpp", "Login Unsuccessfull ");
                ex.printStackTrace();
            }