如何使用Smack Android库向服务器发送XMPP IQ节?

时间:2017-10-04 05:36:48

标签: xmpp ejabberd smack multiuserchat

我想使用Smack Android Library将以下IQ节发送到Ejabberd XMPP Server。我怎么发送?

<iq from='hag66@shakespeare.example'
    to='coven@muc.shakespeare.example'
    type='set'
    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>   

     <subscribe xmlns='urn:xmpp:mucsub:0'
             nick='mynick'
             password='roompassword'>
    <event node='urn:xmpp:mucsub:nodes:messages' />
    <event node='urn:xmpp:mucsub:nodes:affiliations' />
    <event node='urn:xmpp:mucsub:nodes:subject' />
    <event node='urn:xmpp:mucsub:nodes:config' />  
  </subscribe> 
  </iq>

我希望订阅MUC / Sub活动,如https://docs.ejabberd.im/developer/xmpp-clients-bots/proposed-extensions/muc-sub/

中所述

1 个答案:

答案 0 :(得分:0)

我已经使用以下代码,请检查可能对您有帮助。

     try {
                IQ iq=new IQ("mynick","urn:xmpp:mucsub:0") {
                    @Override
                    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
                        xml.setEmptyElement();
                        return xml;
                    }
                };
                iq.setType(IQ.Type.get);
                //add your urn:xmpp:muclight:0#affiliations extension element
                iq.addExtension();
                iq.setTo(AppConstants.CHAT_HOSTNAME);
                iq.setFrom(connection.getUser());
                debugLog("ping send stanza:"+iq.toXML());
                connection.sendStanza(iq);
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }