PacketListener

Hello the following problem arises to me. I am making an application that takes messages. What I do is the following thing:

PacketFilter filtro = new AndFilter(new PacketTypeFilter(Message.class), new ToContainsFilter(con.getConnectionID()));

PacketListener Listener = new PacketListener() {

public void processPacket(Packet packet) {

Message Msg = (Message)packet;

txtMsg.setText(Msg.getBody());

}

};

con.addPacketListener(mylistener, to filter);

The problem is that it does not show the body to me.

It wanted To know that it is what I am making bad.

The problem is likely the ToContainsFilter on con.getConnectionID(). What are you trying to do there? The server will only send messages that are addressed to you, and I think you misunderstand what the connection ID is used for (it’'s not really that useful of a thing at all, really).

Regards,

Matt

This good, if I do the following thing at the time of login, does not work to me either. As I can solve this?

con = new XMPPConnection(“jabber.org”);

con.login(UI,pwd);

con.DEBUG_ENABLED=true;

PacketFilter filter = new PacketTypeFilter(Message.class);

PacketListener listener = new PacketListener() {

public void processPacket(Packet packet) {

Message message = (Message)packet;

System.out.println(packet.getFrom());

System.out.println(message.getBody());

}

};

con.addPacketListener(listener, filter);

Thanks

Ignacio

That code looks correct to me. Are you actually getting any messages that would be printed? You should be able to tell by looking at the debug window.

-Matt

Ignacio,

As you told me by email you are trying to receive messages that were sent to you while you were offline. Your code snippet looks good but since I don’'t have your whole code I think that may be the problem is that you are finishing the “program” too soon so you cannot receive the stored messages.

You can try the following code which is very similar to your code but adds some delays at the end so you can get some time in order to recieve the messages before you close the connection (by ending your program). In a real application you won’‘t need those delays since the application won’'t disconnect after the login.

XMPPConnection con = new XMPPConnection(host, port);
    con.login(username, password, resource);
    PacketFilter filter = new PacketTypeFilter(Message.class);
    PacketListener list = new PacketListener() {
        public void processPacket(Packet packet) {
                Message message = (Message)packet;
                System.out.println(message);
         }};
    con.addPacketListener(list, filter);
    Thread.sleep(500);
    System.out.println("Waiting");
    Thread.sleep(500);
    System.out.println("The end");

Regards,

– Gato

Thanks for the aid, now I can continue.

Regards

Ignacio