我有一个在我的localhost上运行的openfire服务器,我成功地向注册用户发送和接收消息。但是我无法从服务器获得所有用户。我使用没有管理员权限的用户登录。所以我需要在服务器端给予任何许可吗?
我用来获取所有用户的代码是..
if ( xmpp.getConnection()== null || !xmpp.getConnection().isConnected())
return;
try {
UserSearchManager usm = new UserSearchManager(xmpp.getConnection());
Form searchForm = usm.getSearchForm("search." + xmpp.getConnection().getServiceName());
Form answerForm = searchForm.createAnswerForm();
UserSearch userSearch = new UserSearch();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", userName);
ReportedData data = userSearch.sendSearchForm(xmpp.getConnection(), answerForm, "search." + xmpp.getConnection().getServiceName());
for (ReportedData.Row row : data.getRows())
{
arrayList.add(row.getValues("Username").toString());
}
} catch (Exception e) {
e.printStackTrace();
}
我尝试了一些显示使用Roster类的解决方案,但这对我没有帮助。任何人都可以显示我做错了或者我是否需要给予任何许可,因为我没有以管理员身份登录? 我得到的错误是......
org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: remote-server-not-found
谢谢:)
答案 0 :(得分:2)
这就是我从openfire获取所有用户的方式
您实际上必须为用户名
传递通配符(*)这是工作代码
Utils.getConnection() - 我的xmpp连接
public static void getAllXmppUsers()
{
try {
UserSearchManager manager = new UserSearchManager(Utils.getConnection());
String searchFormString = "search." + Utils.getConnection().getServiceName();
Log.d("***", "SearchForm: " + searchFormString);
Form searchForm = null;
searchForm = manager.getSearchForm(searchFormString);
Form answerForm = searchForm.createAnswerForm();
UserSearch userSearch = new UserSearch();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", "*");
ReportedData results = userSearch.sendSearchForm(Utils.getConnection(), answerForm, searchFormString);
if (results != null) {
List<ReportedData.Row> rows = results.getRows();
for (ReportedData.Row row : rows) {
Log.d("***", "row: " + row.getValues("Username").toString());
}
} else {
Log.d("***", "No result found");
}
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
试试这段代码。我从this answer
调整了这段代码UserSearchManager usm= new UserSearchManager(xmpp.getConnection());
Form searchForm = usm.getSearchForm("search." +xmpp.getConnection().getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", userName);
ReportedData data = usm
.getSearchResults(answerForm, "search." + xmpp.getConnection().getServiceName());
if (data.getRows() != null) {
for (ReportedData.Row row: data.getRows()) {
for (String jid:row.getValues("jid")) {
System.out.println(jid);
}
}
}
答案 2 :(得分:0)
Smack用于创建客户端。客户端由一个用户使用。用户通常无权访问服务器的所有用户。但是,用户确实有联系人列表或名单,您可以在其中添加其他用户。