【问题标题】:PrivacyListManager.getInstanceFor(xmppConnection) returns null while blocking user in xmpp asmackPrivacyListManager.getInstanceFor(xmppConnection) 在 xmpp asmack 中阻止用户时返回 null
【发布时间】:2015-02-18 08:05:03
【问题描述】:

我正在尝试在 xmpp asmack 中阻止用户,以下是其代码:

try {
        PrivacyListManager privacyManager = PrivacyListManager
                .getInstanceFor(MyXMPPConnection.getXMPPConnection());
        if (privacyManager == null) {
            return false;
        }
        String Black_List = "blockedList";
        PrivacyList[] plists = privacyManager.getPrivacyLists();
        if (plists.length == 0) {// No blacklisted or is not listed, direct getPrivacyList error
            List<PrivacyItem> items = new ArrayList<PrivacyItem>();
            Log.i("", "addToPrivacyList plists.length==0");
            PrivacyItem newitem = new PrivacyItem("jid", false, 100);
            newitem.setValue(name);
            items.add(newitem);

            privacyManager.updatePrivacyList(Black_List, items);
            privacyManager.setActiveListName(Black_List);
            return true;
        }

        PrivacyList plist = privacyManager.getPrivacyList(Black_List);
        if (plist != null) {
            String ser = "@" + connection.getServiceName();

            List<PrivacyItem> items = plist.getItems();
            for (PrivacyItem item : items) {
                String from = item.getValue().substring(0,
                        item.getValue().indexOf(ser));
                Log.i("",
                        "addToPrivacyList item.getValue=" + item.getValue());
                if (from.equalsIgnoreCase(name)) {
                    items.remove(item);
                    break;
                }
            }

            PrivacyItem newitem = new PrivacyItem("jid", false, 100);
            newitem.setValue(name + "@"
                    + connection.getServiceName());
            items.add(newitem);
            Log.i("", "addToPrivacyList item.getValue=" + newitem.toXML());
            Log.i("", "deleteFromPrivacyList items size=" + items.size());
            privacyManager.updatePrivacyList(Black_List, items);
            privacyManager.setActiveListName(Black_List);

        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }

我将privacyManager 设为空

PrivacyListManager privacyManager = PrivacyListManager
                    .getInstanceFor(MyXMPPConnection.getXMPPConnection());

连接良好,我正在努力解决这个问题。 SO帖子都没有帮助我。我想知道为什么privacyManager 会为空。请帮我解决问题。提前致谢。

【问题讨论】:

  • Smack 是开源的,你为什么不调试代码来确定它返回 null 的原因?

标签: android xmpp openfire smack asmack


【解决方案1】:

我解决了这个问题,我不知道为什么privacylist类没有加载到jvm onload中。所以我明确地将该类加载为Class.forName(PrivacyListManager.class.getName());`

 try {
        ProviderManager.getInstance().addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
        Class.forName(PrivacyListManager.class.getName());
}

【讨论】:

    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多