【问题标题】:Smack Presence Doesn't WorkSmack 存在不起作用
【发布时间】:2012-06-26 19:33:11
【问题描述】:

实际上,我使用 smack API 编写了一个 IM 服务(继承了谷歌聊天)。但是当我想打印好友列表和他们的存在时,编译模式显示所有存在不可用,但在调试模式下它显示真正的可用性!

我的代码是...

1- 创建连接

public boolean openConnection() {
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "mail.google.com");
    this.connection = new XMPPConnection(connectionConfiguration);
    try {
        this.connection.connect();
    } catch (XMPPException e) {
        // TODO: Send Error Information To Programmer's Email Address
    }
    if(this.connection.isConnected()) {
        this.roster = this.connection.getRoster();
        this.roster.addRosterListener(new RosterListener() {
            public void entriesAdded(Collection<String> addresses) {}
            public void entriesDeleted(Collection<String> addresses) {}
            public void entriesUpdated(Collection<String> addresses) {}
            public void presenceChanged(Presence presence) {}
        });
        return true;
    }
    return false;
}

2-登录

public boolean login(String jid, String password) {
    try { 
        this.connection.login(jid, password, "smack");
    } catch (XMPPException e) {
        // TODO: Send Error Information To Programmer's Email Address
    }
    if(this.connection.isAuthenticated()) return true;
    return false;
}

3-好友列表

public void buddiesList() {
    Collection<RosterEntry> rosterEntries = this.roster.getEntries();
    for(RosterEntry rosterEntry: rosterEntries) {
        System.out.println(rosterEntry.username() + " === " + this.roster.getPresence(rosterEntry.getUser()));
    }
}

4- 实现

public static void main(String args[]) {
    IMService imService = new IMService();
    imService.openConnection();
    imService.login("google account", "password");
    imService.buddiesList();
}

【问题讨论】:

    标签: java smack


    【解决方案1】:

    您的 RosterListener 不执行任何操作。当收到状态消息等信息时,您必须在此处放置代码以更新您的花名册。

    您正在检索的状态是创建时状态的快照。要使状态保持最新,您必须对 RosterListener 进行实际编码。这在Javadoc for the getPresence() 方法中有明确说明。

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-01
        • 2011-08-28
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-10
        • 1970-01-01
        • 2016-07-12
        相关资源
        最近更新 更多