Sending and Receiving custom XMPP IQs with aSmack on Android

邮差的信 提交于 2019-11-30 09:51:51

问题


Background:

I have implemented one to one chat using aSmack for XMPP on Android. I am also able to send and receive IQ messages.

The issue is:

I am unable to send and receive custom IQ messages. for example if i want to send an IQ

<iq type='get'
    to='ssmack@web.mystudios.com/mack'>
<query xmlns='http://jabber.org/protocol/disco#items'/>
</iq>

aSmack works fine for this IQ as it is not custom, but if i change the name space here from disco#items to Match it will not work it will send back server a response stating

<error code='503'
       type='cancel'>
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>

and this response is send from my client. but i tried to debug it, i put break points on all receiving and sending packets code. but it does not enter there.

My code for receiving packet is:

connection.addPacketListener(new PacketListener() {

                @Override
                public void processPacket(Packet p) {
                    // TODO Auto-generated method stub
                    String fromName1 = StringUtils.parseBareAddress(p.getFrom());
                    Log.i("XMPPClient", "Got text [" + p.toXML() + "] from [" + fromName1 + "]");

                    m1=p.getFrom();


                    mHandler.post(new Runnable() {
                        public void run() {
                            setListAdapter();
                            recieve.setText(m1);
                        }
                    });

I guess i need to add some listeners to get the custom response. can somebody guide me through that?


回答1:


The code is incomplete. addPacketListener() takes two arguments.

I suspect you don't register a provider for the custom IQ on the receiving side, that's why it returns <service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>

You may want to read some documentation:

  • Provider Architecture: Packet Extensions and Custom IQ's
  • Brief Tutorial on IQ Providers



回答2:


You need to use ServiceDiscoveryManager and register your custom namespace like this:

ServiceDiscoveryManager sm = ServiceDiscoveryManager.getInstanceFor(connection);
sm.addFeature("your:namespace");

Look at Smack sources, all internal IQ handlers add themselves as feature, match incoming query packets by namespace and build result or error reply.



来源:https://stackoverflow.com/questions/22405405/sending-and-receiving-custom-xmpp-iqs-with-asmack-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!