4

我已连接到服务器(Xmpp),但无法在我的 psi 客户端发送和接收数据包

这是我的代码片段

POSClientIQ posclientiq = new POSClientIQ();
    posclientiq.connectXMPPServer();
    posclientiq.processMessage();
   }

   public void processMessage()
   {  try{

      final  IQ iq1 = new IQ() {
     public String getChildElementXML() {
      return "<iq type='get' from ='sam'><query xmlns='jabber:iq:roster'></query></iq>";
    }
  };

  iq1.setType(IQ.Type.GET);
 // PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(iq1.getPacketID()));
  connection.sendPacket(iq1);

  System.out.println("Message send");
4

3 回答 3

2

如果您有一个自定义查询并且您想使用您的 IQ 实现,那么:

final IQ iq = new IQ() {
public String getChildElementXML() { 
return "<query xmlns='http://jabber.org/protocol/disco#info'/>"; // here is your query
//this returns "<iq type='get' from='User@YourServer/Resource' id='info1'> <query xmlns='http://jabber.org/protocol/disco#info'/></iq>";
 }};
// set the type
iq.setType(IQ.Type.GET);
// send the request
connection.sendPacket(iq); 

如您所见,您在这里有您的自定义查询,并且您使用 Smack 设置您的 IQ 的其余部分,例如设置类型。请注意,Smack 会根据您登录的 JID 为您填写“发件人”。

于 2012-02-15T10:22:45.160 回答
2

getChildElementXML() 返回标签。如果您使用的是 Smack,那么您不需要编写自己的 IQ 实现,除非它是自定义查询。对于您的情况,要查询名册,请使用RosterPacket

于 2010-04-08T13:17:40.427 回答
0
//To retrieve archive msges from server..

MyCustomIQ iq = new MyCustomIQ();
      iq.setType(IQ.Type.set);
     mConnection.sendIqWithResponseCallback(iq, new PacketListener() {
                @Override
                public void processPacket(Packet packet) throws SmackException.NotConnectedException {
                    Log.i("Send IQ with Response", "****** message " + packet);
                }
            }, new ExceptionCallback() {
                @Override
                public void processException(Exception exception) {
                  exception.printStackTrace();
                    Log.i("IO archjieve Exception",""+ exception.getMessage());
                }
            }, 5000);

      mConnection.sendPacket(new Presence(Presence.Type.available));
            PacketTypeFilter filter=new PacketTypeFilter(org.jivesoftware.smack.packet.Message.class);
            PacketListener myListener=new PacketListener(){
                public void processPacket(Packet packet){
                   if(((Message) packet).getType().equals(Message.Type.chat))
                    {
                        ((Message) packet).getBody();
                    }
                    else if(((Message) packet).getType().equals(Message.Type.normal))
                    {
                        DefaultPacketExtension pacExten=PacketUtil.packetExtensionfromCollection(packet.getExtensions(), "result", "urn:xmpp:mam:0");
                        String strMsg=pacExten.getValue("body");
                    }
                }
            }
             ;
           mConnection.addPacketListener(myListener, filter);


    //My Custom IQ
    class MyCustomIQ extends IQ {

            String token;


            protected MyCustomIQ() {
            super("query","urn:xmpp:mam:0");
            }



            @Override
            protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
    //  String queryId = prefix + Long.toString(new AtomicLong().incrementAndGet());
                xml.attribute("queryid",queryId);
                xml.rightAngleBracket();
                return xml;
            }


        }


//You may get the response in PacketListerener sometimes so put debug in that also
于 2015-11-30T13:00:19.933 回答