无法使用 smack、openfire 获取名册的存在
我是 smack API 的新手。我正在尝试开发一个聊天应用程序,我试图在其中设置和获取状态。
当我更改用户的存在时,它工作得很好,并且反映在 Openfire 服务器中。
但是,当我尝试获取用户的存在状态时,即使他在 openfire 中的存在状态显示为“可用”,我总是将其状态设置为“不可用”。
我正在使用以下代码来设置状态。
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Online, Programmatically!");
presence.setPriority(24);
presence.setMode(Presence.Mode.available);
user.getConnection().sendPacket(presence);
我正在使用名册类来获得存在,如下所示。
Roster roster = avatar.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for(RosterEntry rosterEntry: entries) {
String user = rosterEntry.getUser();
Presence presence = roster.getPresence(user);
System.out.println("Presence : "+presence); // 1
System.out.println("Presence type: "+presence.getType()); // 2
System.out.println("Presence mode: "+presence.getMode()); // 3
}
第 1 行始终给出“不可用”,而第 2 行和第 3 行始终给出 null
我无法找出此问题的原因。请帮我解决这个问题。
提前致谢。
I am new to smack API. I am trying to develop a chat application where I was trying for setting and getting the presence.
When I change the presence of a user, its working perfectly fine and it is getting reflected in the Openfire Server.
But when I tries to get the Presence of a user, I am always getting the status as 'unavailable' even if his presence in openfire is showing as 'available'.
I am using the following code to set the status.
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("Online, Programmatically!");
presence.setPriority(24);
presence.setMode(Presence.Mode.available);
user.getConnection().sendPacket(presence);
I am using the Roster class to get the presence as follows.
Roster roster = avatar.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for(RosterEntry rosterEntry: entries) {
String user = rosterEntry.getUser();
Presence presence = roster.getPresence(user);
System.out.println("Presence : "+presence); // 1
System.out.println("Presence type: "+presence.getType()); // 2
System.out.println("Presence mode: "+presence.getMode()); // 3
}
Line No 1 alwasys gives 'unavailable' while line number 2 and 3 always give null
I am not able to figure out the cause of this problem. Please help me to resolve this issue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用RosterListener是解决这个问题的正确方法。代码没有理由应该有 Thread.sleep() 才能使其正常工作。
(来源:http://www.igniterealtime.org/builds/ smack/docs/latest/documentation/roster.html)
Using
RosterListener
is the proper solution to this problem. There is no reason that code should have aThread.sleep()
in order to make it work properly.(source: http://www.igniterealtime.org/builds/smack/docs/latest/documentation/roster.html)
问题是,在立即登录后,用户的存在需要一些时间才能更新。因此,在登录和调用在线好友函数之间应该有一个 thread.sleep() 几秒钟。然后将检索在线联系人。我这样做了并且能够找回它们。
登录后使用
Thread.sleep(5000);
也可以在方法的开头使用
the problem is that after logging in immediately, it is gonna take some time for the presence of users to get updated.So between logging in and calling the online buddies function there should be a thread.sleep() for a few seconds.Then the online contacts will be retrieved. I did that and was able to retrieve them.
after login use
Thread.sleep(5000);
use in the beginiing of the method also
我遇到了同样的问题,并搜索了一段时间才发现问题所在。事实上,您不需要执行 Thread.sleep()。问题是您没有“权限”来获取其他用户的状态。
要解决这个问题,只需进入Openfire admin -> >您的用户选项 -> Roster // 然后只需将您想要获得状态的好友的订阅设置为“both”(两个用户都可以查看彼此的状态)。
希望这有帮助。
编辑:实际上,您需要在从连接获取名册之前添加 Thread.sleep() 。如果没有 Thread.sleep(),有时它可以工作,有时则不行......
I had the same problem and searched for a while before finding what the problem was. In fact, you don't need to do a Thread.sleep(). The problem is that you don't have the "permission" to get the Presence of other users.
To solve the problem, just go in Openfire admin -> your user options -> Roster // Then just set the subscription of the buddy you wanna get the presence to "both" (both users can view each other presence).
Hope that is helps.
Edit : In fact you need to add a Thread.sleep() before getting the roster from the connection. Without the Thread.sleep(), sometimes it works, sometimes not...
我修复了它添加:
之后:
Ref: Smack 4.1.0 android Roster not显示
I fixed it adding:
after:
Ref: Smack 4.1.0 android Roster not displaying
这个完整的代码
This full code