2016-12-12 32 views
3

我正在使用jivesoftware Smack SDk即時聊天功能。 爲了創建連接我使用下面的代碼,Android with Smack - 如何獲取在線用戶列表?

XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder(); 
    config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); 

    config.setServiceName("world-pc"); 
    config.setHost(serverAddress); 
    config.setPort(5222); 
    config.setDebuggerEnabled(true); 
    XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true); 
    XMPPTCPConnection.setUseStreamManagementDefault(true); 
    connection = new XMPPTCPConnection(config.build()); 
    XMPPConnectionListener connectionListener = new XMPPConnectionListener(); 
    connection.addConnectionListener(connectionListener); 
    connection.connect(); 
    connection.login("username","password"); 

及其工作令人驚訝的罰款。 現在的事情是,我想獲取特定用戶的在線狀態或獲取所有在線用戶的列表。 我已經嘗試了很多來自堆棧溢出的解決方案,但沒有爲我工作。 我的一個解決方案,我試圖,

Presence presence = new Presence(Presence.Type.available); 
connection.sendPacket(presence); 
Roster roster = xmppConnection.getRoster(); 
Collection<RosterEntry> entries = roster.getEntries(); 
Presence presence; 

for(RosterEntry entry : entries) { 
presence = roster.getPresence(entry.getUser()); 

System.out.println(entry.getUser()); 
System.out.println(presence.getType().name()); 
System.out.println(presence.getStatus()); 
} 

這將返回我一個名單,但狀態爲空的所有用戶。 請有人幫我準確的解決方案。

謝謝

+0

您的客戶是否發送Presence? – MrPk

+0

是的..請檢查編輯的問題 –

回答

2

存在它是由一個TYPE作出(如:Presence.Type.availablePresence.Type.unavailable)和用戶自定義可空status(如或「今天,我很高興」或者「在工作權的「Hello World!」現在」)。

要設置狀態,只需發送之前設置:

Presence presence = new Presence(Presence.Type.available); 
presence.setStatus("Online and ready to chat"); 
connection.sendStanza(presence); //or old one: connection.sendPacket(presence) 
+0

我不想設置我的狀態.. 我想要其他用戶的狀態, 而我沒有找到任何方法像「sendStanza」連接對象 –

+0

每個用戶只能設置自己的「狀態」,所以基本上如果所有設置自己的狀態,你將能夠得到它作爲一個字符串不爲空。當然,你必須管理getStatus爲null的可能性(檢查並設置爲「」)。如果你沒有找到任何「connection.sendStanza()」方法只使用舊的「sendPacket」,你正在使用舊版本的SMACK。 – MrPk

+0

好吧,讓我試試看你的建議 –

3

你可以使用Presence.Type.subscribe才能知道(即用戶),另一個用戶的狀態:

而「another_user」應以同樣的方式批准您的請求:

+0

ok,並且是否有任何偵聽器偵聽用戶狀態更改? –

+0

@Jay Vyas嘗試RosterListener – MrPk

相關問題