2
I am using smack/asmack library for chatting purpose in android.

I am using below code for request/accept, connected users,

获取用户的存在和通知。但它不能正常工作。

1.  I have made connection and loged in by below code

public static boolean XMPPConnect() {
        try {
            config = new ConnectionConfiguration(Constant._hostName, 5222);
            SmackConfiguration.setPacketReplyTimeout(1000*60);
            connection = new XMPPConnection(config);
            config.setSASLAuthenticationEnabled(false);
            connection.connect();
        } catch (Exception e) {
            e.printStackTrace();
                        return false;
        }
        return true;
    }


    public static boolean XMPPLogin(String uname, String password) {
        try {
            connection.login(uname, password);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

2.   I have requested user by create entry in roster by below code
    try {
Roster roster = XMPPSmackConnection.getInstance().connection.getRoster();
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);

//Presence response = new Presence(Presence.Type.subscribe);     //response.setTo(_username);                                              //XMPPSmackConnection.getInstance().connection.sendPacket(response);
                    roster.createEntry(_username+Constant._hostNameWithAt, _username, null);

    } catch (Exception e) {
      e.printStackTrace();
    }

3.  I am accepting the request by below code

       try{
Roster roster = XMPPSmackConnection.getInstance().connection.getRoster();               roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);             _username=_username+Constant._hostNameWithAt;
                        roster.createEntry(_username+Constant._hostNameWithAt, _username, null);
       }catch(Exception e){
       }


And I also making request and accepting by subscribe and unsubscribe by

Presence response = new Presence(Presence.Type.subscribe);
response.setTo(_username);
XMPPSmackConnection.getInstance().connection.sendPacket(response);

Presence response = new Presence(Presence.Type.subscribed);
response.setTo(_username);
XMPPSmackConnection.getInstance().connection.sendPacket(response);

Presence response = new Presence(Presence.Type.unsubscribe);
response.setTo(_username);
XMPPSmackConnection.getInstance().connection.sendPacket(response);

Presence response = new Presence(Presence.Type.unsubscribed);
response.setTo(_username);
XMPPSmackConnection.getInstance().connection.sendPacket(response);

And another problem is that I am not getting updated roster instantly from openfire server.

try{
Thread.sleep(5000);
Roster roster = XMPPSmackConnection.getInstance().connection.getRoster();
    Presence presence;
    if (roster != null) {
    Collection<RosterEntry> entries = roster.getEntries();
    /*   Fetch roster buddies  */
    for (RosterEntry r : entries) {
    presence = roster.getPresence(r.getUser());
    String Usertype = presence.getType().toString();
      if(r.getType().name()=="both"){  
            // For fetch connected  buddies
            Constant._connectedUserNameList.add(r.getUser()); 
      }
    }
}catch(Exception e){}


Can anybody suggest me best way..

Thank You. 
4

0 回答 0