2

实际上,我使用 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();
}
4

2 回答 2

1

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

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

于 2012-06-27T13:20:46.733 回答
0

将听众添加到您的花名册可能会更好:

https://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/rosterexchange.html

于 2015-07-20T14:59:16.657 回答