名为tester
的用户在其名单中添加了另一位用户sanika
。sanika
的订阅模式设为 Roster.SubscriptionMode.accept_all
即可。但是当tester
检查sanika
的状态时,他会subscribe
。为什么会这样?
在函数main中,sanika
设置其状态,在函数connectTester
测试器中,尝试获取sanika
的状态。
public static void main(String[] args) {
try {
Connection connection = new XMPPConnection("localhost");
connection.connect();
connection.login("sanika", "tester");
Roster r = connection.getRoster();
r.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
Presence p = new Presence(Presence.Type.available);
p.setStatus("Having Lunch :)");
connection.sendPacket(p);
connectTester();
Thread.sleep(30000);
} catch(Exception exc) {
exc.printStackTrace();
}
}
public static void connectTester() {
try {
Connection connection = new XMPPConnection("localhost");
connection.connect();
connection.login("tester", "tester");
Roster r = connection.getRoster();
RosterEntry re = r.getEntry("sanika@sanika.com");
System.out.println(re.getStatus().toString());
// PRINTS SUBSCRIBE
}catch(Exception exc) {}
我在哪里犯了错误?
注意: tester
将sanika
添加为:
String group[] = {"Friend List"};
r.createGroup("Friend List");
r.createEntry("sanika@sanika.com", "sanika", group);
答案 0 :(得分:0)
我将两个用户添加到彼此的名单中,如下所示
//subscribe to eachother
connection.login("tester", ...);
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo("sanika@sanika.com");
connection.sendPacket(subscribe);
connection.disconnect();
...
connection.connect();
connection.login("sanika", ...);
subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo("tester@localhost");
connection.sendPacket(subscribe);
connection.disconnect();
另请注意,您似乎同时使用sanika@sanika.com
和sanika@localhost
(login
中的第一个main
- 语句