1

我在 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;
    }

我已按照教程实施 PrivacyList。任何人都可以帮忙吗?

4

2 回答 2

0

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

于 2011-04-04T06:48:37.653 回答
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>
于 2011-04-04T12:16:49.677 回答