从 android 连接到 XMPP 服务器

Connecting to XMPP server from android

如果我 运行 来自 eclipse 的 class 作为 java 应用程序,但当我在 android 中使用相同的 class 时,相同的代码有效studio 我得到以下 RuntimeException.

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

日志:

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.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.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

我被困在这里,真的不明白问题是什么。有帮助吗?

Android 运行时没有 javax.security.sasl 包,升级你的 Smack 库,android

有自定义 SASL 实现

可能您应该升级您的库或编译您的 gradle。

一些信息:What is Gradle in Android Studio?

你好,如果你想为工作室更新你的库,这是一个很好的选择,但如果你也想在工作室中使用相同的代码,那么你可以访问此代码 here 并检查你是否在做正确或 not.Some ssl 权限问题检查这里正确的方法

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

谢谢

希望对您有所帮助。