android:无法创建与 openfire 的连接

android : cannot create connection to openfire

我正在尝试在 ConnectionAsyncTask class 中创建连接以开火。 我正在使用 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.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.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.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:292)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor.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

我不知道它对我有什么帮助..但是,包括下面几行 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;
    }
}

还是谢谢你。

我有同样的错误。

我的依赖项是:

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"

错误消失了。

希望对您有所帮助!