Smack getPrivacyList方法抛出classCastException

时间:2011-04-04 05:34:04

标签: android smack

我在Android应用中使用Smack进行基于XMPP的通信。我正在尝试使用PrivacyManager创建隐私列表。我已成功创建了一个PrivacyList,但是当我尝试访问该列表时,它返回ClassCastException。这是代码:

PrivacyListManager privacyManager;
privacyManager = PrivacyListManager.getInstanceFor(connection);
PrivacyList privacyList = privacyManager.getPrivacyList("msg_block_list");

调查一下,我发现异常发生在以下行的PrivacyListManager.java类中的getRequest方法中:

Privacy privacyAnswer =
            (Privacy) response.nextResult(SmackConfiguration.getPacketReplyTimeout());

这是getRequest方法:

private Privacy getRequest(Privacy requestPrivacy) throws XMPPException {
        // The request is a get iq type
        requestPrivacy.setType(Privacy.Type.GET);
        requestPrivacy.setFrom(this.getUser());

        // Filter packets looking for an answer from the server.
        PacketFilter responseFilter = new PacketIDFilter(requestPrivacy.getPacketID());
        PacketCollector response = connection.createPacketCollector(responseFilter);

        // Send create & join packet.
        connection.sendPacket(requestPrivacy);

        // Wait up to a certain number of seconds for a reply.
        Privacy privacyAnswer =
            (Privacy) response.nextResult(SmackConfiguration.getPacketReplyTimeout());

        // Stop queuing results
        response.cancel();

        // Interprete the result and answer the privacy only if it is valid
        if (privacyAnswer == null) {
            throw new XMPPException("No response from server.");
        }
        else if (privacyAnswer.getError() != null) {
            throw new XMPPException(privacyAnswer.getError());
        }
        return privacyAnswer;
    }

我已按照this教程实施PrivacyList。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

PacketCollector的方法getResult()返回Packet个对象,但它可以是从Packet类对象继承的任何一个,Message或{{1}或Presence(在IQ情况下,您可以使用IQ强制转换,因为(privacy)Privacy的子类。在您的情况下,似乎您获得的对象存在于另一个层次结构中,而不是IQ类。

答案 1 :(得分:0)

我发现了这个问题。缺少XML文件smack.providers。我创建了包含以下内容的文件,但它确实有效。

<?xml version="1.0"?> 
<!-- Providers file for default Smack extensions -->
<smackProviders> 
 <!-- Privacy -->
    <iqProvider>
        <elementName>query</elementName>
        <namespace>jabber:iq:privacy</namespace>
        <className>org.jivesoftware.smack.provider.PrivacyProvider</className>
    </iqProvider>    
</smackProviders>