2016-12-12 10 views
3

私はjacksoftware 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()); 
} 

これは私のリストを返しますが、ステータスは、すべてのユーザーの場合はnullです。 正確な解決策を教えてください。 「!Hello World」のユーザー(などから、カスタムNULL可能statusまたは右または「仕事で「今日、私は幸せです」:

は、それがTYPEPresence.Type.availableまたはPresence.Type.unavailableなど)によって作られて、あなたに

+0

クライアントはプレゼンスを送信しますか? – MrPk

+0

はい。編集した質問を確認してください –

答えて

2

プレゼンスをありがとう今 ")。

単に送信する前にそれを設定し、ステータスを設定するには:

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

各ユーザーは自分で設定できます"ステータス"、基本的にすべて自分のステータスを設定すれば、nullではない文字列として取得できます。もちろん、あなたはnullの可能性をgetStatusとして管理しなければならない(それをチェックして ""に設定する)。古い "sendPacket"メソッドを使用するだけのconnection.sendStanza()メソッドが見つからない場合は、SMACKの古いバージョンを使用しています。 – MrPk

+0

私はあなたの提案と同じように試してみましょう –

3

あなたは、(ユーザーである)別のユーザーの状況を知るためにPresence.Type.subscribeを使用することができます。

そして、 "another_user"は同じ方法でリクエストを承認する必要があります:

+0

から開始する必要がある "ステータスの意味"については、ユーザーのステータスの変更をリッスンするリスナーはいますか? –

+0

@Jay Vyas try RosterListener – MrPk

関連する問題