AnsweredAssumed Answered

How to write a listener to listen namespace: jabber:iq:roster

Question asked by Soosky on Dec 2, 2015
Latest reply on Dec 6, 2015 by dengd

Hi, everyone:


   Below code to login XMPP Server. when i call conn.login(), will send a IQ stanza(<iq><query xmlns='jabber:iq:roster'/></iq>) to server to require roster list.

I want to write a listener to listen when roster IQ(type='result') stanza return.


   I just know how to write a listener to listen IQ stanza like this :StanzaFilter stanzaFilter = new StanzaTypeFilter(IQ.class);


  Below is my code:


  XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();


  .setUsernameAndPassword(username, password)






  SASLAuthentication.registerSASLMechanism(new SASLMechanism() {



  protected SASLMechanism newInstance() {

  return this;




  public int getPriority() {

  return 0;




  public String getName() {

  return null;




  protected byte[] getAuthenticationText() throws SmackException {

  return null;




  public void checkIfSuccessfulOrThrow() throws SmackException {





  protected void authenticateInternal(CallbackHandler cbh) throws SmackException {





  try {

  conn = new XMPPTCPConnection(;

  // connect



  // add roster listener

  roster = Roster.getInstanceFor(getConn());




     How to write a listenr to listen iq stanza which sub-element named query with namespace "iq:jabber:roster", as below:

    <iq type="result" id="Y8P5d-6" to="wangke@">

                    <query xmlns="jabber:iq:roster">

                        <item jid="liangye@" subscription="both"/>

                        <item jid="wangsy@" subscription="none"/>

                        <item jid="wangying@" subscription="to"/>

                        <item jid="wangzc@" subscription="to"/>

                        <item jid="sung@" subscription="to"/>




  StanzaFilter stanzaFilter = new StanzaTypeFilter(IQ.class);

  StanzaListener myListener = new StanzaListener() {


       public void processPacket(Stanza stanza) throws NotConnectedException {

            String from = stanza.getFrom();

            String to = stanza.getTo();

            System.out.println("from=" + from + " to=" + to + " stanza=" + stanza);


            // Will initialize roster list....




  conn.addAsyncStanzaListener(myListener, stanzaFilter);


  // login