public class JabberRPCProvider implements IQProvider {
public IQ parseIQ(final XmlPullParser parser) throws Exception { final StringBuffer buffer = new StringBuffer();
// skip the <query> tag by calling parser.next() while (true) { switch (parser.next()) { case XmlPullParser.TEXT: // We need to escape characters like & and < buffer.append(StringUtils.escapeForXML(parser.getText())); break;
case XmlPullParser.START_TAG: buffer.append('<' + parser.getName() + '>'); break;
case XmlPullParser.END_TAG: if ("query".equals(parser.getName())) { // don't save the </query> end tag return new JabberRPC(buffer.toString().trim()); } else { buffer.append("</" + parser.getName() + '>'); break; } default: } } }
}
3.在smack中注册IQProvider . java代码为: org.jivesoftware.smack.provider.ProviderManager.addIQProvider("query", "jabber:iq:rpc", new JabberRPCProvider());
4.smack客户端例子:
try { XMPPConnection con = new XMPPConnection("127.0.0.1", 5222); con.login("username", "password"); //注册IQProvider org.jivesoftware.smack.provider.ProviderManager.addIQProvider( "query", "jabber:iq:rpc", new JabberRPCProvider()); // 监听所有的包 con.addPacketListener(new PacketListener() { public void processPacket(Packet packet) { if (packet instanceof org.jivesoftware.smack.packet.IQ) { org.jivesoftware.smack.packet.IQ iq = (org.jivesoftware.smack.packet.IQ) packet; System.out.println("rpc内容为 " + iq.getChildElementXML()); } }
}, new org.jivesoftware.smack.filter.PacketFilter() { public boolean accept(Packet arg0) { return true; }