【发布时间】:2015-02-18 08:05:03
【问题描述】:
我正在尝试在 xmpp asmack 中阻止用户,以下是其代码:
try {
PrivacyListManager privacyManager = PrivacyListManager
.getInstanceFor(MyXMPPConnection.getXMPPConnection());
if (privacyManager == null) {
return false;
}
String Black_List = "blockedList";
PrivacyList[] plists = privacyManager.getPrivacyLists();
if (plists.length == 0) {// No blacklisted or is not listed, direct getPrivacyList error
List<PrivacyItem> items = new ArrayList<PrivacyItem>();
Log.i("", "addToPrivacyList plists.length==0");
PrivacyItem newitem = new PrivacyItem("jid", false, 100);
newitem.setValue(name);
items.add(newitem);
privacyManager.updatePrivacyList(Black_List, items);
privacyManager.setActiveListName(Black_List);
return true;
}
PrivacyList plist = privacyManager.getPrivacyList(Black_List);
if (plist != null) {
String ser = "@" + connection.getServiceName();
List<PrivacyItem> items = plist.getItems();
for (PrivacyItem item : items) {
String from = item.getValue().substring(0,
item.getValue().indexOf(ser));
Log.i("",
"addToPrivacyList item.getValue=" + item.getValue());
if (from.equalsIgnoreCase(name)) {
items.remove(item);
break;
}
}
PrivacyItem newitem = new PrivacyItem("jid", false, 100);
newitem.setValue(name + "@"
+ connection.getServiceName());
items.add(newitem);
Log.i("", "addToPrivacyList item.getValue=" + newitem.toXML());
Log.i("", "deleteFromPrivacyList items size=" + items.size());
privacyManager.updatePrivacyList(Black_List, items);
privacyManager.setActiveListName(Black_List);
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
我将privacyManager 设为空
PrivacyListManager privacyManager = PrivacyListManager
.getInstanceFor(MyXMPPConnection.getXMPPConnection());
连接良好,我正在努力解决这个问题。 SO帖子都没有帮助我。我想知道为什么privacyManager 会为空。请帮我解决问题。提前致谢。
【问题讨论】:
-
Smack 是开源的,你为什么不调试代码来确定它返回 null 的原因?
标签: android xmpp openfire smack asmack