1

我正在尝试让 aSmack 在我的项目中工作。在eclipse中创建了一个标准项目,添加了jar来构建路径/库。开始使用 XMPP 类,但我不确定是否应该做其他事情来正确设置项目(稍后),因为它不起作用。在 aService中,我开始AsnycTask连接到 google talk 并发送一条聊天消息。

conn1 = new XMPPConnection(config);
// connecting to the server
try {
    conn1.connect();
    Log.v(LOG_TAG, "XMPP connected");
} catch (XMPPException xe) {
    msg = "XMPPException during connect(): " + xe.getMessage();
    Log.v(LOG_TAG, msg);
}

// login
try {
    conn1.login(USERNAME, PASSWORD, RESOURCE);
    msg = "login ok";
    Log.v(LOG_TAG, msg);
} catch (XMPPException xe) {
    msg = "XMPPException login(): " + xe.getMessage();
    Log.v(LOG_TAG, msg);

    StackTraceElement[] st = xe.getStackTrace();
    for (int i = 0; i<st.length; i++) { 
        msg = xe.getStackTrace()[i].toString();
        Log.v(LOG_TAG, msg);
    }
}

// creating a chat and sending messages
try {
     ChatManager chatmanager = conn1.getChatManager();
     msg = "creating chat";
     Log.v(LOG_TAG, msg);
     Chat chat = chatmanager.createChat(BUDDY_ADDRESS, new MessageListener() {

        @Override
        public void processMessage(Chat chat, Message message) {
            String msg = "incoming chat: " + message;
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
            Log.v(LOG_TAG, msg);
        }
    });
    chat.sendMessage("ahoj");
} catch (XMPPException xe) {
    msg = "XMPPException during connect(): " + xe.getMessage();
    Log.v(LOG_TAG, msg);
}

(请忽略我愚蠢的异常处理,我还在学习它:-) connect() 方法应该可以工作,因为它不会抛出异常,在 Logcat howewer 中,在“XMPP connected”消息之前,有一个错误,然后是 login() 方法抛出的异常:

03-01 15:13:19.970: WARN/System.err(4465): stream:error (text)
03-01 15:13:19.970: WARN/System.err(4465):     at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:261)
03-01 15:13:19.970: WARN/System.err(4465):     at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
03-01 15:13:19.975: WARN/System.err(4465):     at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:71)
03-01 15:13:24.640: DEBUG/dalvikvm(3089): GC_EXPLICIT freed 610 objects / 39760 bytes in 216ms
03-01 15:13:26.370: DEBUG/NetworkLocationProvider(2020): onDataConnectionStateChanged 3
03-01 15:13:26.380: DEBUG/MobileDataStateTracker(2020): replacing old mInterfaceName (pdp0) with pdp0 for hipri
03-01 15:13:26.395: DEBUG/MobileDataStateTracker(2020): replacing old mInterfaceName (pdp0) with pdp0 for supl
03-01 15:13:26.400: DEBUG/MobileDataStateTracker(2020): default Received state= CONNECTED, old= CONNECTED, reason= (unspecified), apnTypeList= default,supl
03-01 15:13:31.570: DEBUG/dalvikvm(2806): GC_EXPLICIT freed 173 objects / 10752 bytes in 207ms
03-01 15:13:34.550: VERBOSE/Smacktest_01(4465): XMPP connected
03-01 15:13:36.600: DEBUG/dalvikvm(2583): GC_EXPLICIT freed 1551 objects / 87688 bytes in 236ms
03-01 15:13:39.580: VERBOSE/Smacktest_01(4465): XMPPException login(): No response from the server.
03-01 15:13:39.585: VERBOSE/Smacktest_01(4465): org.jivesoftware.smack.NonSASLAuthentication.authenticate(NonSASLAuthentication.java:73)
03-01 15:13:39.605: VERBOSE/Smacktest_01(4465): org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:369)
03-01 15:13:39.605: VERBOSE/Smacktest_01(4465): org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:230)
03-01 15:13:39.620: VERBOSE/Smacktest_01(4465): com.biroalex.smacktest_01.SmackTest_01Service$AsyncXMPPConn.doInBackground(SmackTest_01Service.java:105)
03-01 15:13:39.620: VERBOSE/Smacktest_01(4465): com.biroalex.smacktest_01.SmackTest_01Service$AsyncXMPPConn.doInBackground(SmackTest_01Service.java:1)
03-01 15:13:39.635: VERBOSE/Smacktest_01(4465): android.os.AsyncTask$2.call(AsyncTask.java:185)
03-01 15:13:39.640: VERBOSE/Smacktest_01(4465): java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-01 15:13:39.655: VERBOSE/Smacktest_01(4465): java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-01 15:13:39.655: VERBOSE/Smacktest_01(4465): java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-01 15:13:39.660: VERBOSE/Smacktest_01(4465): java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-01 15:13:39.665: VERBOSE/Smacktest_01(4465): java.lang.Thread.run(Thread.java:1096)
03-01 15:13:39.705: VERBOSE/Smacktest_01(4465): creating chat

所以问题是:) 如果我做错了什么,或者项目没有为 aSmack 库在 android 上成为用户做好适当的准备?

有 aSmack 作者的 github https://github.com/rtreffer/asmack,他在其中提到了构建环境,这对我来说完全是中文的。我要创建一个新的构建环境吗?我怎么做?

感谢您的耐心等待!

4

4 回答 4

1

IIRC,如果您想使用 gtalk 和 smack,您必须提供完整的 JID 作为用户名:user@gmail.com

于 2011-08-21T19:00:13.690 回答
0

Google Talk 似乎只在轮询模式下工作: http: //credentiality2.blogspot.com/2010/03/xmpp-asmack-android-google-talk.html

于 2011-03-01T14:28:32.747 回答
0

似乎我的代码可以与其他开放的 XMPP 提供程序一起使用,所以我的代码中可能没有严重错误,也没有我的构建环境或其他任何东西,而且很可能不在 aSmack 库中:) 我会四处寻找尝试连接到谷歌时会发生什么,但问题目前无效。

于 2011-03-03T07:32:10.703 回答
0

我看到了这个问题并找到了答案,或者当我得到这个异常时,至少找到了一个对我有用的答案。创建连接时,请执行以下操作:

    ConnectionConfiguration config = new ConnectionConfiguration(SERVER_HOST, SERVER_PORT);
    config.setTruststoreType("BKS");

希望能帮助到你 :)

于 2012-08-01T12:37:22.813 回答