简体   繁体   English

如何在Android Studio中使用Smack与Openfire XMPP Server进行联网

[英]How to use Smack with Android Studio to network with Openfire XMPP Server

I'm new at Android programming, about two months now. 我是Android编程的新手,大约两个月了。 I have spent quite a bit of time trying to figure out the solution before posting here. 在发布此处之前,我花了很多时间试图找出解决方案。 So, I will go straight into the question and after that, I will explain what had not worked for me. 所以,我会直接回答这个问题,之后,我会解释一些对我没用的东西。

The Question is as follows (it is as per title): 问题如下(按标题):

How do I use Smack 4.1.5 with Android Studio 2.1.2 to network with Openfire XMPP Server 4.0.2? 如何将Smack 4.1.5与Android Studio 2.1.2一起使用以与Openfire XMPP Server 4.0.2联网? Ideally, a short, bare bones piece of code that logs on and sends and receives chats would be appreciated. 理想情况下,登录并发送和接收聊天的简短,简单的代码片段将不胜感激。 Otherwise, strong pointers would be most welcome. 否则,强烈的指针将是最受欢迎的。 (Did I omit a library? Is there something wrong with the try-n-catch?) (我省略了库吗?try-n-catch有什么问题吗?)

The MainActivity.java file is as follows (I have commented out the last bit of code in order to zoom in on the problem code. The best I can tell, the problem is at the line just before the comment starts (/*). However, this is strange as this line is virtually the same as the one recommended by Smack documentation. That's why I am asking for help and let me say here: "Thanks in advance".) MainActivity.java文件如下所示(为了放大问题代码,我已经注释掉了最后一段代码。我能说的最好,问题出在评论开始之前的行(/ *)。然而,这很奇怪,因为这条线几乎与Smack文档推荐的那条线相同。这就是我寻求帮助的原因,让我在这里说:“提前谢谢”。)

(Of cos, I have gotten a Openfire XMPP server up and running on my PC. I had also tested spark on the Openfire XMPP server and it connected and sent and received chats okay.): (首先,我已经在我的PC上启动并运行了一个Openfire XMPP服务器。我还测试了Openfire XMPP服务器上的火花,它连接并发送和接收聊天没问题。):

package com.eg.an_smackandchatclc;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class MainActivity extends AppCompatActivity {
//public static final String HOST = "208.68.163.218"; //write your host name
//public static final int PORT = 5222;
private static final String TAG = "MainActivity.java";
//Log.e(TAG, "declaration");
@Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("onCreate");
    Log.e(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.println("onCreate");
    Log.e(TAG, "setContentView");
    AbstractXMPPConnection conn1 = new XMPPTCPConnection("admin", "admin", "desktop-pc");
}}
/*
    AbstractXMPPConnection conn1 = new XMPPTCPConnection("username", "password", "jabber.org");
    System.out.println("onCreate");
    Log.e(TAG, "conn1");
}}
*/
/*
        try {
        System.out.println("onCreate");
        Log.e(TAG, "try");
        conn1.connect();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    connect();
    System.out.println("onCreate");
    Log.e(TAG, "connect()");
}

The Logcat (Error messages only) is as follows: Logcat(仅限错误消息)如下:

08-06 08:08:46.405 18663-18663/com.eg.an_smackandchatclc E/MainActivity.java: onCreate
08-06 08:08:46.539 18663-18663/com.eg.an_smackandchatclc E/MainActivity.java: setContentView

08-06 08:08:46.672 18663-18663/com.eg.an_smackandchatclc E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.eg.an_smackandchatclc, PID: 18663
                                                                       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:400)
                                                                           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.AbstractXMPPConnection.<clinit>(AbstractXMPPConnection.java:97)
                                                                           at com.eg.an_smackandchatclc.MainActivity.onCreate(MainActivity.java:62)
                                                                           at android.app.Activity.performCreate(Activity.java:6658)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2692)
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6044)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
08-06 08:08:46.672 18663-18663/com.eg.an_smackandchatclc E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.naming.directory.InitialDirContext" on path: DexPathList[[dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-support-annotations-24.0.0_87b336946543c1f787a3f9b83be41366f9cdbb84-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-tcp-4.1.5_b0d53c1047a32e900a08ccf219813c5bc2e618c2-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-sasl-provided-4.1.5_084129969859dd9e8f37122e5ef57f0cfd22a46c-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-sasl-javax-4.1.0_0ab65da887f48d8d5e384912160eaa1e0908d186-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-resolver-minidns-4.1.5_0ae177b49eafb38d7b12353d2eda6cc149b4fe43-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-resolver-javax-4.1.0_d306adbf26933e7f728102e3672f3f90637ba1f4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-java7-4.1.0_dcb0e760478344b0ca249594ceb7373304752bae-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-im-4.1.5_1ecc996e048dc5efba3acddae625e28aedfd704e-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-extensions-4.1.5_4eacc0e2ea2f430835f5aa7fef27c8a657ed75b2-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-core-4.1.5_21299a3731f708b27e7f5f12a5bc6ebf265c7a80-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-android-extensions-4.1.5_ce6630a8dabc9b47414ad59ad934c206078b768c-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-android-4.1.5_d8cd382b84de7e021e8ac21a53fd3e9213386ed4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-minidns-0.1.7_4e64c8580258e0c1f1f12d1e7412dad623952782-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-jxmpp-util-cache-0.4.2_108c360a78086927507dd4d9b7a2ad0580db469b-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-jxmpp-core-0.4.2_ee26bb5026023b646554c0bf9ba14c39ffd9a0f5-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-internal_impl-24.0.0_f8e857843a2f013dbed9c33184b5eae71c5fa909-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.0.0_c612cdcfcff025f7f2ab5366116677fa236ef2b7-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-support-v4-24.0.0_cdc3a4409b42b56d3a7c52c568071a86579f9e63-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-appcompat-v7-24.0.0_50c6a3f385fbb5da5a5fbefc3749fa4
08-06 08:08:46.672 18663-18663/com.eg.an_smackandchatclc E/AndroidRuntime: abfcd41b4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-24.0.0_5eae71faeeb4e4b2f9cdde61d342e39a2cdcfcb5-classes.dex"],nativeLibraryDirectories=[/data/app/com.eg.an_smackandchatclc-2/lib/x86_64, /system/lib64, /vendor/lib64]]
                                                                           at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                           at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                            ... 23 more

I have set up the app build.gradle to import all the appropriate libraries (or at least I think I have). 我已经设置了app build.gradle来导入所有相应的库(或者至少我认为我有)。 They include: 他们包括:

<Android API 24 Platform>
<1.8>
android-android-24
animated-vector-drawable-24.0.0
appcompat-v7-24.0.0
hamcrest-core-1.3
junit-4.12
jxmpp-core-0.4.2
jxmpp-util-cache-0.4.2
minidns-0.1.7
smack-android-4.1.5
smack-android-extensions-4.1.5
smack-core-4.1.5
smack-extensions-4.1.5
smack-im-4.1.5
smack-java7-4.1.0
smack-resolver-javax-4.1.0
smack-resolver-minidns-4.1.5
smack-sasl-javax-4.1.0
smack-sasl-provided-4.1.5
smack-tcp-4.1.5
support-annotations-24.0.0
support-v4-24.0.0
support-vector-drawable-24.0.0

The above is the basic information relating to the question. 以上是与该问题有关的基本信息。

The following was the "homework" that I tried to do before posting this question. 以下是我在发布此问题之前尝试做的“作业”。

I had poured through every word of the Smack documentation ( http://download.igniterealtime.org/smack/docs/latest/documentation/ ). 我已经倾听了Smack文档中的每一个字( http://download.igniterealtime.org/smack/docs/latest/documentation/ )。 Initially, I tried using the "few lines of code" suggested in the documentation: 最初,我尝试使用文档中建议的“几行代码”:

```java AbstractXMPPConnection connection = new XMPPTCPConnection("mtucker", "password", "jabber.org");
connection.connect().login();
Chat chat = ChatManager.getInstanceFor(connection) .createChat("jsmith@jivesoftware.com", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Howdy!"); ```

( http://download.igniterealtime.org/smack/docs/latest/documentation/overview.html ) http://download.igniterealtime.org/smack/docs/latest/documentation/overview.html

Unfortunately, no joy at all. 不幸的是,根本没有快乐。

Then, I resorted to testing that one line: (Ha-ha-ha. Stack Overflow says that the code I wanted to quote looks like spam so I put this in its place. Now it's not funny anymore cos even the previous sentence looked at spam to SO. Let's see if these two sentences will allow me to get this post through.) (Ha-ha. Stack Overflow says I need at least 10 reputation to post more than 2 links so the link I wanted to put here had to go. Anyway, I got said one line of code from the "Getting Started" page of the Smack documentation.) 然后,我尝试测试一行:(哈哈哈.Dack Overflow说我想引用的代码看起来像垃圾邮件,所以我把它放在它的位置。现在它不再有趣,甚至上一句话看着垃圾邮件到SO。让我们看看这两句话是否允许我通过这篇文章。)(哈哈哈。Stack Overflow说我需要至少10个声望才能发布超过2个链接,所以我想放在这里的链接必须无论如何,我从Smack文档的“入门”页面得到了一行代码。)

Still, in vain. 仍然是徒劳的。

Trust me, I have tried all the other solutions suggested on the net (including elsewhere on Stack Overflow) but I have failed with all of them. 相信我,我已经尝试了网上建议的所有其他解决方案(包括Stack Overflow上的其他地方),但我对所有这些解决方案都失败了。 Each time, some error message or other would arise. 每次都会出现一些错误消息或其他错误消息。 Alternatively, the code sample given would entail having more coding chops than I had to make work. 或者,给出的代码示例需要比我必须编写的代码更多的编码。 For example, the code sample may refer to a method which was not defined or to a class that had to be created on my own. 例如,代码示例可以引用未定义的方法或者必须由我自己创建的类。

So, once again, thanks in advance for any help on this. 所以,再次感谢您提供任何帮助。

Use android libraries 使用android库

  • org.igniterealtime.smack:smack-android:4.1.6 org.igniterealtime.smack:咂嘴,安卓4.1.6
  • org.igniterealtime.smack:smack-tcp:4.1.6 org.igniterealtime.smack:咂嘴-TCP:4.1.6
  • org.igniterealtime.smack:smack-im:4.1.6 org.igniterealtime.smack:咂嘴-IM:4.1.6

these libraries are enough to get started. 这些库足以开始使用。 all java libraries are not compatible with android, specially javax package. 所有java库都与android不兼容,特别是javax包。

I had the same problem with Smack 4.1.x (NoClassDefFoundError on InitialDirContext). 我遇到了与Smack 4.1.x相同的问题(InitialDirContext上的NoClassDefFoundError)。

Try using Smack 4.2, in build.gradle: 尝试在build.gradle中使用Smack 4.2:

    compile "org.igniterealtime.smack:smack-android-extensions:4.2.0-alpha3"
    compile "org.igniterealtime.smack:smack-tcp:4.2.0-alpha3"

And this is how I initialized the connection. 这就是我初始化连接的方式。

    private void initializeConnection() {
    try {
        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setHost(mServerAddress)
                .setPort(mServerPort)
                .setXmppDomain(JidCreate.domainBareFrom(mServiceName))
                .setUsernameAndPassword(mUserLogin, mUserPassword)
                .build();

        mXmppTcpConnection = new XMPPTCPConnection(config);

        XmppConnectionListener connectionListener = new XmppConnectionListener();
        mXmppTcpConnection.addConnectionListener(connectionListener);
    } catch (XmppStringprepException e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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