【问题标题】:Block user in android using xmpp + smack + openfire使用 xmpp + smack + openfire 在 android 中阻止用户
【发布时间】:2015-06-08 11:21:27
【问题描述】:

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

我正在尝试实现一个功能,它会阻止特定用户,但它对我不起作用。它不会给出任何错误或异常。

我的代码是

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 和 openfire???
  • 你的问题解决了吗???
  • No this PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, userName,false, 1l) 构造函数不在 PrivacyItem 类中
  • 你使用的是哪个版本的 smack 和 openfire???
  • qsmack 的状态请尝试 Smack 4.1.0,它比 qsmack 更稳定,功能更多,您可以参考此获取更多信息github.com/igniterealtime/Smack/wiki/…

标签: android xmpp openfire


【解决方案1】:

使用 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>

希望这能解决您的问题

【讨论】:

    【解决方案2】:
        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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 2013-09-07
      • 2019-05-02
      • 2023-03-11
      • 2015-04-12
      • 1970-01-01
      • 2017-04-18
      • 2014-02-04
      相关资源
      最近更新 更多