无法使用 smack、openfire 获取名册的存在

发布于 2024-08-15 07:30:39 字数 1110 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等风也等你 2024-08-22 07:30:39

使用RosterListener是解决这个问题的正确方法。代码没有理由应该有 Thread.sleep() 才能使其正常工作。

Roster roster = con.getRoster();
roster.addRosterListener(new RosterListener() {
    // Ignored events public void entriesAdded(Collection<String> addresses) {}
    public void entriesDeleted(Collection<String> addresses) {}
    public void entriesUpdated(Collection<String> addresses) {}
    public void presenceChanged(Presence presence) {
        System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
    }
});

(来源: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 a Thread.sleep() in order to make it work properly.

Roster roster = con.getRoster();
roster.addRosterListener(new RosterListener() {
    // Ignored events public void entriesAdded(Collection<String> addresses) {}
    public void entriesDeleted(Collection<String> addresses) {}
    public void entriesUpdated(Collection<String> addresses) {}
    public void presenceChanged(Presence presence) {
        System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
    }
});

(source: http://www.igniterealtime.org/builds/smack/docs/latest/documentation/roster.html)

上课铃就是安魂曲 2024-08-22 07:30:39

问题是,在立即登录后,用户的存在需要一些时间才能更新。因此,在登录和调用在线好友函数之间应该有一个 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

攒一口袋星星 2024-08-22 07:30:39

我遇到了同样的问题,并搜索了一段时间才发现问题所在。事实上,您不需要执行 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...

最近可好 2024-08-22 07:30:39

我修复了它添加:

 if (!roster.isLoaded()) 
  roster.reloadAndWait();

之后:

 Roster roster = Roster.getInstanceFor(connection);

Ref: Smack 4.1.0 android Roster not显示

I fixed it adding:

 if (!roster.isLoaded()) 
  roster.reloadAndWait();

after:

 Roster roster = Roster.getInstanceFor(connection);

Ref: Smack 4.1.0 android Roster not displaying

甜妞爱困 2024-08-22 07:30:39

这个完整的代码

 public void getRoaster(final Callback<List<HashMap<String, String>>> callback) {
    final Roster roster = Roster.getInstanceFor(connection);
    boolean success = true;

    if (!roster.isLoaded())
        try {
            roster.reloadAndWait();
        } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException | InterruptedException e) {
            android.util.Log.e(AppConstant.PUBLIC_TAG, TAG + " " + e.getMessage());
            success = false;
        }

    if (!success) {
        if (callback != null) {
            callback.onError(new Throwable());
        }
    }

    Collection<RosterEntry> entries = roster.getEntries();
    List<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>(entries.size());
    for (RosterEntry entry : entries) {
        HashMap<String, String> map = new HashMap<String, String>(3);
        Presence presence = roster.getPresence(entry.getUser());

        map.put(ROASTER_KEY, entry.getName());
        map.put(ROASTER_BARE_JID, entry.getUser());
        map.put(PRESENCE_TYPE, presence.isAvailable() == true ? PRESENCE_ONLINE : PRESENCE_OFFLINE);
        maps.add(map);
    }

    if (maps != null && maps.size() > 0 && callback != null) {
        callback.onSuccess(maps);
    } else {
        callback.onError(new Throwable());
    }
}

This full code

 public void getRoaster(final Callback<List<HashMap<String, String>>> callback) {
    final Roster roster = Roster.getInstanceFor(connection);
    boolean success = true;

    if (!roster.isLoaded())
        try {
            roster.reloadAndWait();
        } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException | InterruptedException e) {
            android.util.Log.e(AppConstant.PUBLIC_TAG, TAG + " " + e.getMessage());
            success = false;
        }

    if (!success) {
        if (callback != null) {
            callback.onError(new Throwable());
        }
    }

    Collection<RosterEntry> entries = roster.getEntries();
    List<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>(entries.size());
    for (RosterEntry entry : entries) {
        HashMap<String, String> map = new HashMap<String, String>(3);
        Presence presence = roster.getPresence(entry.getUser());

        map.put(ROASTER_KEY, entry.getName());
        map.put(ROASTER_BARE_JID, entry.getUser());
        map.put(PRESENCE_TYPE, presence.isAvailable() == true ? PRESENCE_ONLINE : PRESENCE_OFFLINE);
        maps.add(map);
    }

    if (maps != null && maps.size() > 0 && callback != null) {
        callback.onSuccess(maps);
    } else {
        callback.onError(new Throwable());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文