不叫嫌消息监听器和连接不稳定 [英] Smack message listener not called and connection unstable

查看:309
本文介绍了不叫嫌消息监听器和连接不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code应该监听消息:

I have the following code which should listen for messages:

ChatManager chatmanager = ChatManager.getInstanceFor(xmppManager.getConnection());

chat = chatmanager.createChat(otherJabberId);

chat.addMessageListener(new ChatMessageListener() {
    @Override
    public void processMessage(Chat chat, Message message) {
        Log.e("message trigger", message.getBody());
    }
}

但我从来没有登录触发器。

But my Log never triggers.

不过,我有 setDebuggerEnabled(真)在我的code和下面显示出来: 15 10-31:41:51.264 28889-28993 / com.lfdversluis.buurapp D / SMACK:RECV(0):其中p消息=test@app.buur.nu/Smack类型=从=testje @应用聊天ID =53 .buur.nu / Gajim&GT;&LT;身体GT;试验&LT; /身体GT;&lt;请求xmlns=\"urn:xmpp:receipts\"/><thread>277945c1-772a-4d4b-8e1a-274153cfb8a6</thread></message>

However, I have setDebuggerEnabled(true) in my code and the following shows up: 10-31 15:41:51.264 28889-28993/com.lfdversluis.buurapp D/SMACK: RECV (0): <message to="test@app.buur.nu/Smack" type="chat" id="53" from="testje@app.buur.nu/Gajim"><body>test</body><request xmlns="urn:xmpp:receipts"/><thread>277945c1-772a-4d4b-8e1a-274153cfb8a6</thread></message>

因此​​,在收到该消息。我已经检查和 otherJabberId 变量是正确的。这根本就不是触发听者。什么是更奇怪的,有时它只是正常工作。

So the message is received. I have checked and the otherJabberId variable is correct. It is simply the listener not triggering. What's even more weird, sometimes it just works fine.

另一个问题,我是不是能够发送邮件。

Another Issue I have is not being able to send messages.

在这里,我有聊天设置如上,并使用以下code发送消息:

Here, I have the chat setup as above and use the following code to send a message:

try {  
    chat.sendMessage(text.trim());  
    DataBaseManager db = new DataBaseManager(ChatActivity.this);  
    db.addMessageToDB(model);  

    addMessageToScreen(newMessage);  

} catch (SmackException.NotConnectedException ignored) {  
    XMPPManager manager = XMPPManager.getInstance();  
    manager.reconnect(); // Maybe we need to reconnect due to an interrupt and retry..  
    try {  
        chat.sendMessage(text.trim());  
        DataBaseManager db = new DataBaseManager(ChatActivity.this);  
        db.addMessageToDB(model);  
        addMessageToScreen(newMessage);  
    } catch (SmackException.NotConnectedException e) {  
        e.printStackTrace();  
        Toasteroid.show(ChatActivity.this, "Could not send message. Please try again.", Toasteroid.STYLES.ERROR);  
    }  
}  

和与无法发送消息敬酒将弹出飘飞。因此很明显,我不接,我不能重新要么?

And the toast with the "could not send message" pops-up every now and then. So apparently I am not connected and I cannot reconnect either?

那么,怎样才能使我的连接更加稳定,并确保我的信息被发送?

So how can I make my connection more stable and make sure my messages get sent?

推荐答案

我认为你需要更新你code是这样的: -

I think you need to update you code like this:-

chat.addMessageListener(new MessageListener() {
                 public void processMessage(Chat chat, Message message) {
                     System.out.println("Received message: "
                             + (message != null ? message.getBody() : "NULL"));
                 }
             });

修改 ChatMessageListener()的MessageListener(),其中的MessageListener()是这样的 - > org.jivesoftware.smack.MessageListener;
我希望它的工作。

Change ChatMessageListener() to MessageListener() , where MessageListener() is this -> org.jivesoftware.smack.MessageListener; I hope its work.

这篇关于不叫嫌消息监听器和连接不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆