无法发送和接收 XMPP 消息

Posted

技术标签:

【中文标题】无法发送和接收 XMPP 消息【英文标题】:Unable to send and receive XMPP messages 【发布时间】:2015-05-31 11:01:34 【问题描述】:

我正在尝试通过 android 中的 SMack 库发送和接收 XMPP 消息。但未能做到这一点。即使我成功连接到服务器并获得在线用户也。但无法发送或接收短信。请提出任何解决方案。

代码: XMPP 建立连接:

 try 


            ConnectionConfiguration config = new ConnectionConfiguration("chat.spectratech.in");
            config.setTruststorePath("/system/etc/security/cacerts.bks");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
                config.setTruststoreType("AndroidCAStore");
                config.setTruststorePassword(null);
                config.setTruststorePath(null);
             else 
                config.setTruststoreType("BKS");
                String path = System.getProperty("javax.net.ssl.trustStore");
                if (path == null)
                    path = System.getProperty("java.home") + File.separator + "etc"
                        + File.separator + "security" + File.separator
                        + "cacerts.bks";
                config.setTruststorePath(path);
            
            mXmppConnection = new XMPPConnection(config);
            mXmppConnection.connect();
            mXmppConnection.login(USERNAME, PASSWORD);

            chatApp.setmXmppConnection(mXmppConnection);
        
        catch (final XMPPException e) 
            Log.e(TAG, "Could not connect to Xmpp server.", e);
            return;
        

        if (!mXmppConnection.isConnected()) 
            Log.e(TAG, "Could not connect to the Xmpp server.");
            return;
        

        Log.i(TAG, "Yey! We're connected to the Xmpp server!");
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
        mXmppConnection.getChatManager().addChatListener(new ChatManagerListener() 

            @Override
            public void chatCreated(final Chat chat, final boolean createdLocally) 
                if (!createdLocally) 
                    chat.addMessageListener(new MyMessageListener());
                
            
        );

发送和接收消息:

if (app.getmXmppConnection() != null) 
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            app.getmXmppConnection().addPacketListener(new PacketListener() 
                @Override
                public void processPacket(Packet packet) 
                    Message message = (Message) packet;
                    if (message.getBody() != null) 
                        String fromName = StringUtils.parseBareAddress(message
                                .getFrom());
                        Log.i("XMPPChatDemoActivity", "Text Recieved "
                                + message.getBody() + " from " + fromName);
                        msgs.add(fromName + ":  " + message.getBody());
                        //messages.add(fromName + ":");
                        //messages.add(message.getBody());
                        // Add the incoming message to the list view
                        mHandler.post(new Runnable() 
                            public void run() 
                                adapter.notifyDataSetChanged();
                            
                        );
                    
                
            , filter);
        

        btnSend.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View arg0) 
                String text = edMsgs.getText().toString();
                Message msg = new Message(to, Message.Type.chat);
                msg.setBody(text);

                if(app.getmXmppConnection()!=null)
                    app.getmXmppConnection().sendPacket(msg);
                    msgs.add(text);
                    adapter.notifyDataSetChanged();
                

            
        );

【问题讨论】:

【参考方案1】:

首先你需要登录到一个服务器。

Class Message的链接

发送消息: 我使用 XMPPConnection 类发送 sendPacket。

String message = "Hello friend";
Message msg = new Message(toUserId, Message.Type.chat);
                    msg.setBody(message);
                    connection.sendPacket(msg);

接收消息:

我使用PacketFilter 类只接收聊天消息。 我在消息发送时使用XMPPConnection 类添加侦听器。

PacketFilter chatFilter = new MessageTypeFilter(Message.Type.chat);     
connection.addPacketListener(chatPacketListener, chatFilter);           

PacketListener chatPacketListener = new PacketListener() 

        @Override
        public void processPacket(Packet packet) 
            try 

                Message message = (Message) packet;
                String body = message.getBody();
                String from = StringUtils.parseBareAddress(message.getFrom());

             catch (Exception e) 
        
    ;      

希望对你有帮助。

【讨论】:

以上是关于无法发送和接收 XMPP 消息的主要内容,如果未能解决你的问题,请参考以下文章

XMPP 客户端接收服务器消息

无法在 xmpp 的聊天屏幕中获取接收消息

JACK XMPP 守护进程发送和接收消息

如何在 ios 中使用 XMPP 接收离线消息

XMPP 聊天发送/接收消息同步

xmpp在android应用程序中发送和接收消息