[英]How to get list of offline/online users in chat by using smack api in android
[英]Getting list of all users registered on server with online/offline status using Smack API on android
我使用Smack API連接到ejabberd XMPP服務器-
username = "admin";
password = "admin";
port = "5222";
serviceName = "davids-macbook-pro.local";
host = "192.168.20.8";
config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword(username, password)
.setHost(host)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setServiceName(serviceName)
.setPort(Integer.parseInt(port))
.build();
connection = new XMPPTCPConnection(config);
try {
connection.connect();
connection.login();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
我想獲取所有在線用戶的列表。 這怎么可能 ?
當我嘗試-
Roster roster = Roster.getInstanceFor(connection);
Collection<RosterEntry> entries = roster.getEntries();
Log.i("entry", entries+"");
for (RosterEntry entry : entries) {
}
獲取一個空數組
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
HashMap<String, String> map = new HashMap<String,String>();
Presence entryPresence = roster.getPresence(entry.getUser());
Presence.Type type = entryPresence.getType();
map.put("USER", entry.getName().toString());
map.put("STATUS", type.toString());
Log.e("USER", entry.getName().toString());
usersList.add(map);
}
檢查狀態等於“可用”,則用戶在線,否則用戶離線。
如下所示,用戶在Roaster
為getPresence
。
Roster roster = Roster.getInstanceFor(connection);
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry rosterEntry : entries){
System.out.println("xmpp.Roster.presence "+ roster.getPresence(rosterEntry.getUser()).getType());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.