简体   繁体   中英

Connecting to XMPP server from android

The same code is working if I run the class from eclipse as a java application but when I am using same class in android studio I am getting the following RuntimeException .

java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/security/sasl/Sasl; 

The log :

FATAL EXCEPTION: Thread-62267
Process: com.abcd.abcd, PID: 14825
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/security/sasl/Sasl;
at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:92)
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:319)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:203)
at com.abcd.abcd.XmppManager.login(XmppManager.java:99)
at com.abcd.abcd.Main.initializeXmppConnection(Main.java:78)
at com.abcd.abcd.Main.init(Main.java:72)
at com.abcd.abcd.Main.<init>(Main.java:61)
at com.abcd.abcd.MainActivity$1.run(MainActivity.java:32)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.security.sasl.Sasl" on path: DexPathList[[zip file "/data/app/com.abcd.abcd-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:92) 
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:319) 
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:203) 
at com.abcd.abcd.XmppManager.login(XmppManager.java:99) 
at com.abcd.abcd.Main.initializeXmppConnection(Main.java:78) 
at com.abcd.abcd.Main.init(Main.java:72) 
at com.abcd.abcd.Main.<init>(Main.java:61) 
at com.abcd.abcd.MainActivity$1.run(MainActivity.java:32) 
at java.lang.Thread.run(Thread.java:818) 
Suppressed: java.lang.ClassNotFoundException: javax.security.sasl.Sasl
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 10 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

I am stuck here and really not getting what the problem is. Any help guys?

hi if you want to update your library for studio that is one option is good but if you want to use same code in studio as well then you can visit this code here and check if you are doing correct or not.Some problem with ssl permission check here correct way

private XMPPConnection connect() throws XMPPException, SmackException,
        IOException {
    if ((this.connection != null) && (this.connection.isConnected())) {
        return this.connection;
    }

    ConnectionConfiguration config = new ConnectionConfiguration(HOST1,
            5222);
    SmackConfiguration.DEBUG_ENABLED = true;
    SASLAuthentication.supportSASLMechanism("MD5", 0);
    System.setProperty("smack.debugEnabled", "true");
    config.setCompressionEnabled(false);
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    config.setReconnectionAllowed(true);
    config.setSendPresence(true);
    config.setRosterLoadedAtLogin(true);

    if (Build.VERSION.SDK_INT >= 14) {
        config.setKeystoreType("AndroidCAStore");
        config.setKeystorePath(null);
    } else {
        config.setKeystoreType("BKS");
        String str = System.getProperty("javax.net.ssl.trustStore");
        if (str == null) {
            str = System.getProperty("java.home") + File.separator + "etc"
                    + File.separator + "security" + File.separator
                    + "cacerts.bks";
        }
        config.setKeystorePath(str);
    }
    if (connection == null) {
        this.connection = new XMPPTCPConnection(config);
    }
    this.connection.connect();
    Roster roster = connection.getRoster();
    roster.addRosterListener(new RosterListener() {

        @Override
        public void presenceChanged(Presence arg0) {
            Log.d("deb", "ug");
        }

        @Override
        public void entriesUpdated(Collection<String> arg0) {
            Log.d("deb", "ug");
        }

        @Override
        public void entriesDeleted(Collection<String> arg0) {
            Log.d("deb", "ug");
        }

        @Override
        public void entriesAdded(Collection<String> arg0) {
            Log.d("deb", "ug");
        }
    });
    return this.connection;
}

Thankyou

Hope this will help you.

Android运行时没有javax.security.sasl包,升级你的Smack库,有一个自定义SASL实现的android

Probably you should upgrade your library or compile your gradle.

For some informations: What is Gradle in Android Studio?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM