![](/img/trans.png)
[英]How to restore chat history between two users from Openfire server using Smack in android app
[英]Getting all users from an openfire server using smack android
我有一個運行在本地主機上的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
謝謝 :)
這就是我從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();
}
}
試試這個代碼。 我從這個答案中調整了這段代碼
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);
}
}
}
Smack用於創建客戶端。 客戶端由一個用戶使用。 用戶通常無法訪問服務器的所有用戶。 用戶確實具有聯系人列表或名冊,您可以在其中添加其他用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.