我开发了简单的聊天应用程序,其中使用了 asmack lib 和 openfire 服务器。但是,当我开始使用相同的聊天时,我总是无法获得在线状态,因为该用户在 openfire 服务器上可用。
尝试了各种方法,测试了talk.google.com 服务器的相同代码,它运行良好。在这里,我正在设置用户的存在状态-
Presence presence = new Presence(Presence.Type.available); ;
presence.setStatus("Online, Programmatically!");
presence.setPriority(24);
presence.setMode(Presence.Mode.available);
connection.sendPacket(presence);
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
当我显示名册条目时,我的状态状态为空,并且状态类型不可用,我不知道为什么会发生,尝试了各种方法。我正在同一台机器上的两个不同模拟器上测试我的代码。是因为一样吗?
这是显示名册条目和存在类型的代码
Roster roster = connection.getRoster();
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);
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);
}
@Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub
}
});
try { Thread.sleep(5000); } catch (InterruptedException e) { }
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
Presence subscribed = new Presence(Presence.Type.subscribed);
subscribed.setTo(entry.getUser());
connection.sendPacket(subscribed);
Log.d("JWP", "RosterEntry " + entry);
Log.d("JWP", "User: " + entry.getUser());
Log.d("JWP", "Name: " + entry.getName());
Log.d("JWP", "Status: " + entry.getStatus());
Log.d("JWP", "Type: " + entry.getType());
Presence entryPresence = roster.getPresence(entry.getUser());
Log.d("JWP", "Presence Status: "+ entryPresence.getMode());
Log.d("JWP", "Presence Type: " + entryPresence.getType());
Presence.Type type = entryPresence.getType();
if (type == Presence.Type.available){
Log.d("JWP", "Presence AVIALABLE");
Log.d("JWP", "Presence : " + entryPresence);
}
}