XMPP Smack 4.1.1 UnsupportedIQ错误

时间:2015-06-01 21:56:39

标签: android xmpp chat smack

我正在使用XMPP(smack 4.1.1)制作Android聊天应用程序。 这是我使用的节数据包监听器。

但是当我使用这个监听器时,我得到了UnsupportedIQ错误。 我错了什么?

connection.addAsyncStanzaListener(new StanzaListener() {
    @Override
    public void processPacket(Stanza p) throws SmackException.NotConnectedException {

         if (p.getStanzaId().equals(getTokenId)) {
            IQ iq = (IQ) p;
            if (iq != null && iq.getType().equals(IQ.Type.result)) {

                // here, I want to get xxxxxx value from <value xmlns='jabber:client'>xxxxxxx</value>

            }
         }
    }, new StanzaFilter() {
    @Override
    public boolean accept(Stanza stanza) {
         return true;
    }
});

IQ tokenIQ = new IQ("query", "urn:tmp:token") {
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
     xml.rightAngleBracket();
     return xml;
   }
};

getTokenId = tokenIQ.getStanzaId();
try {
    connection.sendStanza(tokenIQ);
} catch (SmackException.NotConnectedException e) {
    e.printStackTrace();
}

2 个答案:

答案 0 :(得分:1)

在UnparsedIQ类中抛出UnsupportedOperationException &#34;由于未找到提供程序而无法解析的IQ节。&#34;

Pls refer this

我想,在你的案例提供者中为&#34; urn:tmp:token&#34;没找到。

答案 1 :(得分:0)

我解决了这个问题。 这是我的代码。

class TokenIQ extends IQ {
    String token;

    protected TokenIQ(String childElementName) {
        super(childElementName);
    }

    public String getToken() {
        return token;
    }

    @Override
    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        return xml;
    }
}

class TokenProvider extends IQProvider
{
    @Override
    public Element parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {

        TokenIQ tokenIQ = new TokenIQ("value");
        int eventType = parser.next();
        tokenIQ.token = parser.getText();

        return tokenIQ;
    }
}

ProviderManager.addIQProvider("value", "jabber:client", new TokenProvider());

//调用部分

//"<query xmlns='urn:tmp:token'/>"
IQ tokenIQ = new IQ("query", "urn:tmp:token") {

    @Override
    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
            xml.rightAngleBracket();
            return xml;
    }
};

getTokenId = tokenIQ.getStanzaId();
try {
    mConnection.sendStanza(tokenIQ);
} catch (SmackException.NotConnectedException e) {
    e.printStackTrace();
}

//接收部分

mConnection.addAsyncStanzaListener(new StanzaListener() {
    @Override
    public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
        if (packet.getStanzaId() != null && packet.getStanzaId().equals(getTokenId)) {

            TokenIQ iq = (TokenIQ)packet;
            tokenId = iq.getToken();    
        }       
    }
}, new StanzaFilter() {
    @Override
    public boolean accept(Stanza stanza) {
        return true;
    }
});