android:无法创建与openfire

时间:2015-08-20 07:36:25

标签: android xmpp openfire smack

我正在尝试在ConnectionAsyncTask课程中创建与开火的连接。 我正在使用Smack库v4.13

以下是代码:

public class ConnectionAsyncTask extends AsyncTask<Void, Void, Void> {

    public ConnectionAsyncTask() {

    }

    @Override
    protected Void doInBackground(Void... params) {

        XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
        config.setServiceName(HOST);
        config.setHost(HOST);
        config.setPort(PORT);
        config.setDebuggerEnabled(true);

        mConnection = new XMPPTCPConnection(config.build());

        try {
            mConnection.connect();
            Log.i("connect", "connected");
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

        return null;
    }
}

但是上面的代码抛出了以下错误:

08-20 13:03:29.573   3138-26629/com.example.hsoni.corpchat E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
    Process: com.example.hsoni.corpchat, PID: 3138
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:304)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/naming/directory/InitialDirContext;
            at org.jivesoftware.smack.util.dns.javax.JavaxResolver.<clinit>(JavaxResolver.java:50)
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:309)
            at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213)
            at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193)
            at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163)
            at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
            at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116)
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
            at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
            at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder(XMPPTCPConnectionConfiguration.java:80)
            at com.example.hsoni.corpchat.SignUpActivity$ConnectionAsyncTask.doInBackground(SignUpActivity.java:73)
            at com.example.hsoni.corpchat.SignUpActivity$ConnectionAsyncTask.doInBackground(SignUpActivity.java:64)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.naming.directory.InitialDirContext" on path: DexPathList[[zip file "/data/app/com.example.hsoni.corpchat-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.util.dns.javax.JavaxResolver.<clinit>(JavaxResolver.java:50)
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:309)
            at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213)
            at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193)
            at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163)
            at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
            at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116)
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
            at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
            at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder(XMPPTCPConnectionConfiguration.java:80)
            at com.example.hsoni.corpchat.SignUpActivity$ConnectionAsyncTask.doInBackground(SignUpActivity.java:73)
            at com.example.hsoni.corpchat.SignUpActivity$ConnectionAsyncTask.doInBackground(SignUpActivity.java:64)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
    Suppressed: java.lang.ClassNotFoundException: javax.naming.directory.InitialDirContext
            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)
            ... 20 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

注意:我在项目中包含了JAR Files and Requirements列出的所有库。

2 个答案:

答案 0 :(得分:1)

我不知道它对我有什么帮助..但是,包括以下几行build.gradle解决了我的问题:

compile "org.igniterealtime.smack:smack-android:4.1.3"
compile "org.igniterealtime.smack:smack-bosh:4.1.3"
compile "org.igniterealtime.smack:smack-tcp:4.1.3"
compile "org.igniterealtime.smack:smack-im:4.1.3"
compile "org.jxmpp:jxmpp-jid:0.5.0-alpha6"
compile "org.igniterealtime.smack:smack-extensions:4.1.3"

我不知道背后的原因。如果有人知道原因,欢迎您编辑和更新我的答案。

现在这就是我能够建立联系的方式:

public class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        // Create a connection to the jabber.org server on a specific port.
        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setServiceName("my-ip")
                .setHost("my-ip")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setPort(5222)
                .setDebuggerEnabled(true)
                .build();

        AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
        try {
            conn2.connect();
            Log.i("connecting: ", "Connected");
        } catch (SmackException | XMPPException | IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

非常感谢。

答案 1 :(得分:1)

我遇到了同样的错误。

我的依赖是:

compile "org.igniterealtime.smack:smack-android:4.1.7"
compile "org.igniterealtime.smack:smack-tcp:4.1.7"
compile "org.igniterealtime.smack:smack-im:4.1.7"
compile "org.igniterealtime.smack:smack-extensions:4.1.7"

我认为它与JavaxResolver有关,请参阅此thread

我添加了以下依赖项:

compile "org.igniterealtime.smack:smack-bosh:4.1.7"

错误消失了。

希望有所帮助!