8

我想从我的聊天列表中阻止一个特定的朋友XMPP。代码工作正常。没有例外,但我无法阻止用户。我正在使用开放式服务器。我应该在服务器上进行哪些更改?

你们有什么想法吗?

我的代码:

public void XMPPAddNewPrivacyList(Connection connection, String userName) {

    String listName = "newList";

    // Create the list of PrivacyItem that will allow or
    // deny some privacy aspect

    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);
    }
}
4

4 回答 4

5

尝试这个 ...

public boolean blockFriend(String friendName) {

    PrivacyItem item=new PrivacyItem(PrivacyItem.Type.jid,friendName, false, 7);
    PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(connection);
    List<PrivacyItem> list=new ArrayList<PrivacyItem>();
    list.add(item);

    try {
        privacyManager.updatePrivacyList(NEWLIST, list);
        privacyManager.setActiveListName(NEWLIST);
        return true;
    } catch (SmackException.NoResponseException |XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
        e.printStackTrace();
        return false;
    }


}

对于解锁,只需在privacyitem `的对象中将false替换为true

于 2015-05-11T06:15:00.113 回答
0

我认为问题应该是以下之一:

  • 用户名不正确,例如“someuser@myxmppserver.com”。
  • 我的意思是,您没有监听隐私更改,您没有实现 PrivacyListener 接口。
  • 在 PrivacyItem 构造函数中,您不应该使用 PrivacyRule.JID 而不是 PrivacyItem.Type.jid.toString()?。
  • 如果您想阻止朋友,您不应该使用 updatePrivacyList 而不是 createPrivacyList。

我建议您仔细查看文档 Smack 文档

于 2013-09-22T03:59:17.060 回答
0
    // Here function for block user on xmpp

    public boolean blockUser(String userName) {

    String jid = userName@localhost
    String listName = "public";

    // Create the list of PrivacyItem that will allow or
    // deny some privacy aspect

    //ArrayList privacyItems = new ArrayList();

    List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();


    PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, jid, false, 1);
    // item.setValue(userName);
    item.setFilterIQ(false);
    item.setFilterMessage(false);
    item.setFilterPresenceIn(false);
    item.setFilterPresenceOut(false);

    privacyItems.add(item);

    // Get the privacy manager for the current connection.

    // Create the new list.
    PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(XMPPUtils.INSTANCE.connection);


    try {
        privacyManager.updatePrivacyList(listName, privacyItems);
        privacyManager.setActiveListName(listName);

        return true;
    } catch (Exception e) {
        Log.e("PRIVACY_ERROR: ", " " + e.toString());
        e.printStackTrace();
    }

    return false;
}
于 2015-08-27T10:39:38.423 回答
0

隐私是用户阻止来自特定其他用户的通信的一种方法。在 XMPP 中,这是通过管理一个人的隐私列表来完成的。

1 - 为了在服务器中添加一个新列表,客户端可以实现类似的东西:

 

    // Create a privacy manager for the current connection._
    PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(myConnection);
    // Retrieve server privacy lists_
    PrivacyList[] lists = privacyManager.getPrivacyLists();

2 - 为了在服务器中添加一个新列表,客户端可以实现类似的东西:

 

// Set the name of the list_
String listName = "newList";

// Create the list of PrivacyItem that will allow or deny some privacy aspect_
String user = "tybalt@example.com";
String groupName = "enemies";
ArrayList privacyItems = new ArrayList();

PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, user, true, 1);
privacyItems.add(item);

item = new PrivacyItem(PrivacyItem.Type.subscription, PrivacyItem.SUBSCRIPTION_BOTH, true, 2);
privacyItems.add(item);

item = new PrivacyItem(PrivacyItem.Type.group, groupName, false, 3);
item.setFilterMessage(true);
privacyItems.add(item);

// Get the privacy manager for the current connection._
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(myConnection);
// Create the new list._
privacyManager.createPrivacyList(listName, privacyItems);

于 2015-09-11T15:38:11.037 回答