Trying to connect to XMPP server with aSmack on Android gets me “no dns resolver active”

家住魔仙堡 提交于 2019-12-05 23:20:08
amrinder007

I recommend using Smack instead of aSmack. Asmack is a patched and enhanced version of Smack and aSmack is somewhat dead. In Smack code refactoring is done, added some new methods and refactored DNS classes.

See this Smack

UPDATE
After seeing your error log it seems to be the problem of network call on main UI thread

How to resolve NetworkOnMainThread exception?
Use AsyncTask which makes your network call on different thread(in background) rather than on app's main thread.

Move your code to 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) {

    }

}

And now you can execute your AsyncTask:-

new ConnectToXmpp().execute();

With this your network call will be made in background in different thread.

See this AsyncTask

If aSmack tells you that no DNS resolver is active then you most likely did not initialize the static code of aSmack as aSmack's README told you to do.

From the README

In order to work correctly on Android, you need to register Smack's XMPP Providers and Extensions manually and init some static code blocks before you doing any XMPP activty. Calling SmackAndroid.init(Context) (in org.jivesoftware.smack) will do this for you.

Simply call SmackAndroid.init(Context) and you are fine.

This works for me:

            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();
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!