使用 xmpp + smack + openfire 阻止 android 中的用户

Block user in android using xmpp + smack + openfire

在我的聊天应用程序中。我正在使用 Smack 库和 Openfire 服务器。我想阻止特定用户。

我正在尝试实现一个功能,该功能将阻止特定用户,但它不适用于 me.and,它不会给出任何错误或异常。

我的密码是

public void XMPPAddNewPrivacyList(XMPPConnection connection, String userName) {

        String listName = "newList";
        List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();

        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.toString(),
                false, 1);
        item.setValue(userName);
        privacyItems.add(item);

        // Create the new list.

        try {
            PrivacyListManager privacyManager = new PrivacyListManager(connection);
            privacyManager = PrivacyListManager
                    .getInstanceFor(connection);
            privacyManager.createPrivacyList(listName, privacyItems);

        } catch (XMPPException e) {
            System.out.println("PRIVACY_ERROR: " + e);
        }
    }

  XMPPAddNewPrivacyList(XmppConnection.getInstance().getConnection(),
 "91xxxxxxxxxx@xxx.com");

使用 Smack 4.1.0 和 Openfire 3.10.0 你可以实现像下面这样的 Block user

public void XMPPAddNewPrivacyList(XMPPConnection connection, String userName) {

        String listName = "newList";
        List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();

        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid,
            userName,false, 1l);
    privacyItems.add(item);
    // Create the new list.

    try {
        PrivacyListManager privacyManager; 
        privacyManager = PrivacyListManager
                .getInstanceFor(connection);
        privacyManager.createPrivacyList(listName, privacyItems);

    } catch (XMPPException e) {
        System.out.println("PRIVACY_ERROR: " + e);
    }
    }

现在如果你像这样调用上面的函数

XMPPAddNewPrivacyList(XmppConnection.getInstance().getConnection(),
 "91xxxxxxxxxx");    

在 smack 调试器中,您可以观察 iq 节下方

<iq id="5W6tl-27" type="set">
  <query xmlns="jabber:iq:privacy">
    <list name="newList">
      <item action="deny" order="1" type="jid" value="91xxxxxxxxxx"/>
    </list>
  </query>
</iq>

<iq to="xyz@test-xmpp-abc/Smack" id="5W6tl-27" type="result">
  <query xmlns="jabber:iq:privacy">
    <list name="newList">
      <item action="deny" order="1" type="jid" value="91xxxxxxxxxx"/>
    </list>
  </query>
</iq>

希望这能解决您的问题

    public List<String> getBlockedUserList(String userId) { 

    List<String> privacyList = new ArrayList<String>();
    try {
        PrivacyListManager privacyManager = PrivacyListManager
                .getInstanceFor(XMPPUtils.INSTANCE.connection);
        if (privacyManager == null) {
            return privacyList;
        }
        String ser = "@" + XMPPUtils.INSTANCE.XMPPService;
        PrivacyList plist = null;
        try {
            plist = privacyManager.getPrivacyList("public");
        } catch (NoResponseException e) {
            e.printStackTrace();
        } catch (NotConnectedException e) {
            e.printStackTrace();
        }
        if (plist != null) {// No blacklisted or is not listed, direct getPrivacyList error
            List<PrivacyItem> items = plist.getItems();
            for (PrivacyItem item : items) {


                String from = item.getValue().substring(0,
                        item.getValue().indexOf(ser));

                if (userId.equals(from)) {

                    item.isAllow();
                }
                // privacyList.add(from);


            }
        } else {
            return privacyList;
        }
    } catch (XMPPException ex) {
    }
    return privacyList;
}