实际上我是使用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();
}
答案 0 :(得分:1)
您的RosterListener没有做任何事情。这是您在收到状态消息等事项时必须放置代码来更新您的名单的地方。
您正在检索的状态是状态创建时的快照。要使状态保持最新,您必须实际编写RosterListener。 Javadoc for the getPresence()方法明确说明了这一点。
答案 1 :(得分:0)