2015-06-08 57 views
2

阻止用戶在我的聊天應用程序中。我正在使用Smack庫和Openfire服務器。我想阻止特定的用戶。在android中使用xmpp + 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(), 
"[email protected]"); 
+0

什麼嫌版本的Openfire您正在使用??? – Dev

+0

你的問題解決了嗎? – Dev

+0

沒有這個PrivacyItem item =新的PrivacyItem(PrivacyItem.Type.jid, userName,false,1l)構造函數不在PrivacyItem類 – Kinjal

回答

0

隨着啪4.1.0和Openfire的3.10.0可以達到阻止用戶像下面

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

在咂嘴調試器可以觀察以下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="[email protected]/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> 

希望這將解決您的問題

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