Android 使用xmpp+;smack+;明火
在我的聊天应用程序中。我正在使用Smack库和Openfire服务器。我想阻止特定的用户 我正在尝试实现一个功能,它将阻止一个特定的用户,但它不适合我。它不会给出任何错误或异常 我的代码是Android 使用xmpp+;smack+;明火,android,xmpp,openfire,Android,Xmpp,Openfire,在我的聊天应用程序中。我正在使用Smack库和Openfire服务器。我想阻止特定的用户 我正在尝试实现一个功能,它将阻止一个特定的用户,但它不适合我。它不会给出任何错误或异常 我的代码是 public void XMPPAddNewPrivacyList(XMPPConnection connection, String userName) { String listName = "newList"; List<PrivacyItem> priva
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);
}
}
使用Smack 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);
}
}
在smack调试器中,您可以在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="xyz@test-xmpp-abc/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>
希望这能解决您的问题public List getBlockedUserList(String userId){
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;
}
List privacyList=new ArrayList();
试一试{
PrivacyListManager privacyManager=PrivacyListManager
.getInstanceFor(XMPPUtils.INSTANCE.connection);
if(privacyManager==null){
返回私有列表;
}
字符串ser=“@”+XMPPUtils.INSTANCE.XMPPService;
PrivacyList plist=null;
试一试{
plist=privacyManager.getPrivacyList(“public”);
}捕获(无响应异常e){
e、 printStackTrace();
}捕获(未连接异常e){
e、 printStackTrace();
}
如果(plist!=null){//No列入黑名单或未列入黑名单,则直接返回getPrivacyList错误
List items=plist.getItems();
用于(PrivacyItem:items){
字符串from=item.getValue().substring(0,
item.getValue().indexOf(ser));
if(userId.equals(from)){
item.isAllow();
}
//privacyList.add(from);
}
}否则{
返回私有列表;
}
}捕获(XMPPException-ex){
}
返回私有列表;
}
您使用的smack和openfire的哪个版本???问题解决了吗???否此PrivacyItem项=新PrivacyItem(PrivacyItem.Type.jid,用户名,false,1l)构造函数不在PrivacyItem类中您正在使用哪个版本的smack和openfire???安装qsmack请尝试smack 4.1.0。它比qsmack更稳定,具有更多功能。有关更多信息,请参考此版本
<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="xyz@test-xmpp-abc/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>
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;
}