aSmack XMPP lib无法正常工作

时间:2011-03-01 14:24:14

标签: android xmpp smack

我正在尝试让sSck在我的项目中工作。 在eclipse中创建了一个标准项目,添加了jar来构建路径/库。开始使用XMPP类,但我不确定我是否应该做些什么来正确设置项目(稍后),因为它不起作用。 在Service中,我开始AsnycTask连接谷歌对话并发送一条聊天消息。

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连接”消息之前,出现错误,然后是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 lib成为android上的用户?

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

感谢您的耐心等待!

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

答案 2 :(得分:0)

似乎我的代码与其他开放的XMPP提供程序一起工作,所以可能在我的代码,我的构建环境或其他任何东西都没有严重错误,并且很可能不在aSmack lib :) 我会四处寻找尝试连接谷歌时会发生什么,但现在这个问题无效。

答案 3 :(得分:0)

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

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

希望有所帮助:)