Hi, everyone:
Below code to login XMPP Server. when i call conn.login(), will send a IQ stanza() 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();
configBuilder.setDebuggerEnabled(true)
.setUsernameAndPassword(username, password)
.setServiceName(serviceName)
.setPort(port)
.setResource(resource)
.setSecurityMode(SecurityMode.disabled);
SASLAuthentication.registerSASLMechanism(new SASLMechanism() {
@Override
protected SASLMechanism newInstance() {
return this;
}
@Override
public int getPriority() {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
protected byte[] getAuthenticationText() throws SmackException {
return null;
}
@Override
public void checkIfSuccessfulOrThrow() throws SmackException {
}
@Override
protected void authenticateInternal(CallbackHandler cbh) throws SmackException {
}
});
try {
conn = new XMPPTCPConnection(configBuilder.build());
// connect
conn.connect();
// add roster listener
roster = Roster.getInstanceFor(getConn());
roster.addRosterListener(rosterListener);
__ /* __
** How to write a listenr to listen iq stanza which sub-element named query with namespace “iq:jabber:roster”, as below:**
** **
** **
** **
** **
** **
** **
** **
** **
** **
*/
StanzaFilter stanzaFilter = new StanzaTypeFilter(IQ.class);
StanzaListener myListener = new StanzaListener() {
@Override
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
conn.login();