3

I made this as simple as I can in code. I am using asmack library for android in version 8-0.8.3.

My code:

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;
    }
}

It freeze my app and cause me and error: no dns resolver active. There is nothing on the web about it.

I do really hope to get some help or clues.

Also tried like this:

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

public void xmppService() throws 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:

Using smack library instead of asmack gets me the same problem. I get no error log, but after disconnecting debugger I get:

enter image description here

4

3 回答 3

6

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.

于 2013-06-07T09:22:31.090 回答
2

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

于 2013-06-07T04:25:42.667 回答
1

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();
            }
于 2014-09-08T15:13:29.423 回答