0

用户名为在他的名册中tester添加了另一个用户sanika。订阅模式sanika设置为Roster.SubscriptionMode.accept_all。但是当tester检查sanika他的状态时subscribe。为什么会这样?

在 main 函数中,sanika设置她的状态,在函数connectTestertester 中,尝试获取 的状态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);
4

1 回答 1

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.comand sanika@localhost(中的第一个login语句main

于 2013-08-19T10:48:07.183 回答