我想使用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);
}
}
答案 0 :(得分:4)
试试这个......
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答案 1 :(得分:0)
我认为问题应该是以下之一:
我建议您仔细查看文档Smack documentation
答案 2 :(得分: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;
}
答案 3 :(得分: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);