Android中的非常简单的XMPP客户端

时间:2015-10-12 11:20:56

标签: android xmpp

我正在尝试使用XMPP服务器创建一个基本的消息传递基础设施,并且使用Python成功地完成了这项工作。在Python上成功发送和接收小的预定义消息。现在接下来的任务是从Android手机发送类似的消息。注意我真的不需要Xabber样式客户端提供的完整聊天客户端功能。

我是Android的新手,并没有真正使用Java,Eclipse。尽管如此,我已经在Android中采用了一个基本的“Hello World”程序(空白活动模板),并使用从igniterealtime网站获取的一些基本XMPP代码进行了“增强”。所以MainActivity.java程序看起来像这样:

package com.yantrajaal.pm1324;

import java.io.IOException;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
           configBuilder.setUsernameAndPassword("userid", "xxxxxx");
           //configBuilder.setResource("SomeResource");
           configBuilder.setServiceName("adastra.re");

           AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
           // Connect to the server
           try {
            connection.connect();
        } catch (SmackException | IOException | XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           // Log into the server
           try {
                connection.login();
            } catch (SmackException | IOException | XMPPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我所包含的smack库是

/home/hduser/workspace/PM1324/libs/smack-tcp-4.1.4.jar
/home/hduser/workspace/PM1324/libs/smack-android-4.1.4.jar
/home/hduser/workspace/PM1324/libs/android-support-v4.jar
/home/hduser/workspace/PM1324/libs/smack-core-4.1.4.jar

/home/hduser/eclipse-android-sdks/platforms/android-23/android.jar

包含这些库并导入各种类后,程序中没有编译时错误: - )

但是,当我在模拟器中执行程序时,程序会在启动时立即崩溃(停止工作)。即使是“Hello World”字符串也没有出现。

日志的相关部分显示以下内容:

10-12 16:41:42.860: E/AndroidRuntime(893): FATAL EXCEPTION: main
10-12 16:41:42.860: E/AndroidRuntime(893): Process: com.yantrajaal.pm1324, PID: 893
10-12 16:41:42.860: E/AndroidRuntime(893): java.lang.NoClassDefFoundError: org.jxmpp.util.XmppStringUtils
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.provider.ProviderManager.getKey(ProviderManager.java:314)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.provider.ProviderManager.addStreamFeatureProvider(ProviderManager.java:304)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.provider.ProviderManager.addLoader(ProviderManager.java:140)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.initializer.UrlInitializer.initialize(UrlInitializer.java:54)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:232)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
10-12 16:41:42.860: E/AndroidRuntime(893):  at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
10-12 16:41:42.860: E/AndroidRuntime(893):  at com.yantrajaal.pm1324.MainActivity.onCreate(MainActivity.java:22)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.app.Activity.performCreate(Activity.java:5231)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.os.Handler.dispatchMessage(Handler.java:102)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.os.Looper.loop(Looper.java:136)
10-12 16:41:42.860: E/AndroidRuntime(893):  at android.app.ActivityThread.main(ActivityThread.java:5001)
10-12 16:41:42.860: E/AndroidRuntime(893):  at java.lang.reflect.Method.invokeNative(Native Method)
10-12 16:41:42.860: E/AndroidRuntime(893):  at java.lang.reflect.Method.invoke(Method.java:515)
10-12 16:41:42.860: E/AndroidRuntime(893):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-12 16:41:42.860: E/AndroidRuntime(893):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-12 16:41:42.860: E/AndroidRuntime(893):  at dalvik.system.NativeStart.main(Native Method)

如果有人能指导我做错了什么以及如何解决这个问题,我将不胜感激。

提前感谢您提供的任何建议和指导。

新问题/问题

FATAL EXCEPTION和NoClassDefFoundError已得到解决[见下面的第二个答案],但现在我的XMPP调用又遇到了另一个问题。 [我不想在同一个问题上创建另一个问题,所以我继续在这里]

现在程序执行,不会中止但无法连接到XMPP服务器,日志显示以下警告。

10-13 08:48:44.065: W/System.err(1049): org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: 'adastra.re:5222' failed because android.os.NetworkOnMainThreadException
10-13 08:48:44.085: W/System.err(1049):     at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:596)
10-13 08:48:44.085: W/System.err(1049):     at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:830)
10-13 08:48:44.085: W/System.err(1049):     at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:360)
10-13 08:48:44.095: W/System.err(1049):     at com.example.pm707.MainActivity.onCreate(MainActivity.java:32)
10-13 08:48:44.095: W/System.err(1049):     at android.app.Activity.performCreate(Activity.java:5231)
10-13 08:48:44.095: W/System.err(1049):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-13 08:48:44.095: W/System.err(1049):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-13 08:48:44.095: W/System.err(1049):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 08:48:44.095: W/System.err(1049):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 08:48:44.095: W/System.err(1049):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 08:48:44.095: W/System.err(1049):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 08:48:44.095: W/System.err(1049):     at android.os.Looper.loop(Looper.java:136)
10-13 08:48:44.095: W/System.err(1049):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 08:48:44.095: W/System.err(1049):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 08:48:44.095: W/System.err(1049):     at java.lang.reflect.Method.invoke(Method.java:515)
10-13 08:48:44.095: W/System.err(1049):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 08:48:44.095: W/System.err(1049):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 08:48:44.095: W/System.err(1049):     at dalvik.system.NativeStart.main(Native Method)
10-13 08:48:44.095: W/System.err(1049): org.jivesoftware.smack.SmackException$NotConnectedException: Client is not, or no longer, connected
10-13 08:48:44.155: W/System.err(1049):     at org.jivesoftware.smack.tcp.XMPPTCPConnection.throwNotConnectedExceptionIfAppropriate(XMPPTCPConnection.java:334)
10-13 08:48:44.155: W/System.err(1049):     at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:447)
10-13 08:48:44.165: W/System.err(1049):     at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:410)
10-13 08:48:44.165: W/System.err(1049):     at com.example.pm707.MainActivity.onCreate(MainActivity.java:39)
10-13 08:48:44.165: W/System.err(1049):     at android.app.Activity.performCreate(Activity.java:5231)
10-13 08:48:44.165: W/System.err(1049):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-13 08:48:44.165: W/System.err(1049):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-13 08:48:44.165: W/System.err(1049):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
10-13 08:48:44.175: W/System.err(1049):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-13 08:48:44.175: W/System.err(1049):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-13 08:48:44.185: W/System.err(1049):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 08:48:44.185: W/System.err(1049):     at android.os.Looper.loop(Looper.java:136)
10-13 08:48:44.185: W/System.err(1049):     at android.app.ActivityThread.main(ActivityThread.java:5001)
10-13 08:48:44.185: W/System.err(1049):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 08:48:44.185: W/System.err(1049):     at java.lang.reflect.Method.invoke(Method.java:515)
10-13 08:48:44.185: W/System.err(1049):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-13 08:48:44.185: W/System.err(1049):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-13 08:48:44.185: W/System.err(1049):     at dalvik.system.NativeStart.main(Native Method)
10-13 08:48:44.355: D/(1049): HostConnection::get() New Host Connection established 0xb8104c50, tid 1049
10-13 08:48:44.415: W/EGL_emulation(1049): eglSurfaceAttrib not implemented

感谢您解决此问题的任何想法

2 个答案:

答案 0 :(得分:0)

应该可以在运行时中使用Smack jar,但是android.jar - 已经存在于每个Android设备上,并且只能在编译时中访问编译器。 NoClassDefFoundError表示应用无法找到smack库,因为它们包含在目标apk中,因此问题出在您的构建配置中。

答案 1 :(得分:0)

问题已解决如下。 从所有图书馆中取出所有的罐子。 然后我在How to use Smack 4.1 on Android上查看了之前的答案 这导致我Smack Readme and Upgrade Guide。 我下载了python3文件,按照说明创建了artifact.csv文件并运行了python3脚本。这在我的项目文件夹中安装了所有smack 4.1内容。

由于其他错误,该程序仍无法正常工作,但NoClassDefFound的这个问题已解决。