2

I am trying to send and recieve XMPP messages through SMack library in android. But failed to do the same. Even I successfully connected to server and get the online users also. But unable to send or receive text messages. Please suggest any solution regarding the same.

Code: XMPP Establish Connection:

 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());
                }
            }
        });

Send and Receive Messages:

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();
                }

            }
        });
Sanat Pandey
  • 4,081
  • 17
  • 75
  • 132

1 Answers1

1

First you need to login to a Server.

Link for Class Message

Sending Messages: I use with XMPPConnection Class to send sendPacket.

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

Receiving Messages:

I use of the PacketFilter class to recive only chat message. I use of the XMPPConnection Class add listner when message comming.

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) {}
        }
    };      

I hope it help you.

biraj800
  • 150
  • 1
  • 1
  • 9