Issue in blocking user in chatlist using smack and open fire server

I want to block a particular friend from my chat list with XMPP . code works fine. There is no Exception, but I am not able to block a user. I'm using open fire server. what changes should i made on server?

Can u guys have any idea?

My code:

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

try this ...

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


}

and for unblock just replace false with true in object of privacyitem `


I think that the problem should be one of the following:

  • userName isn't correct, for example "someuser@myxmppserver.com".
  • You are not listening for privacy changes I mean, you didn't implement the PrivacyListListener interface.
  • In the PrivacyItem constructor don't you should use PrivacyRule.JID instead PrivacyItem.Type.jid.toString()?.
  • If you wanna block a friend don't you should use updatePrivacyList instead createPrivacyList.
  • I reccomend you take a closer look to the documentation Smack documentation


        // 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;
    }
    
    链接地址: http://www.djcxy.com/p/73956.html

    上一篇: Django与外键用户迁移模型失败

    下一篇: 使用smack和开放式消息服务器拦截用户在聊天列表中的问题