2015-06-01 66 views
0

我正在使用XMPP的Android聊天應用(4.1.1嫌)。 這裏是節包監聽器我用。XMPP啪4.1.1 UnsupportedIQ錯誤

但是,當我用這個監聽我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(); 
} 

回答

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; 
    } 
}); 
1

UnsupportedOperationException異常是從類UnparsedIQ時拋出 「因爲沒有供應商被發現無法解析的IQ節。」

Pls refer this

我想,在你的情況下提供者 「甕:TMP:令牌」 沒有被發現。

+0

這也太 https://www.igniterealtime.org/builds/smack/docs/latest/文檔/ providers.html – Ashishail