0
public boolean connect() {
    String host = "talk.google.com";
    String port = "5222";
    String service = "gmail.com";

    ConnectionConfiguration connConfig = new ConnectionConfiguration(host,
            Integer.parseInt(port), service);
    connection = new XMPPConnection(connConfig);

    try {
        connection.connect();
        log("xmpp connected");
        connection.login("USERNAME@gmail.com", "Password");
        log("user loged in");
        log("loged user : " + connection.getUser());
    } catch (XMPPException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

public void displayBuddyList() {
    String user;
    String status_Message; 
    int state;
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();

    log("Buddy list size :"+entries.size());
    for (RosterEntry r : entries) {

        user=r.getUser();
        status_Message=retrieveStatusMessage(user);
        log("name: "+r.getName()+"  user :"+user+"  Status :"+status_Message+" type :"+retrieveState(user));

    }
}

这是我在服务中编写的代码。我在我的服务类中持有 xmpp 连接对象。从我调用这些方法的活动中。我可以登录,我可以在 logcat 中看到日志。但是当我调用 displayBuddyList 时我无法获取好友列表()。

4

1 回答 1

1
Roster roster = XMPPConnection.getRoster();
        Collection<RosterEntry> entries= roster.getEntries();
        ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
                new VCardProvider());
        VCard card = null;
        for (RosterEntry entry : entries) {
            card = new VCard();
            Presence presencek= roster.getPresence(entry.getUser());
            try {
                card.load(Main.conn, entry.getUser());
            } catch (Exception e) {
                e.printStackTrace();
            }

            String jid = entry.getUser();
             String name = card.getField("FN");
            String status = presencek.getType().name();
            Log.d("Prescence", "" + presencek.getType().name());// //num one log

            byte[] imgs = card.getAvatar();
            if (imgs != null) {
                int len = imgs.length;
                Bitmap img = BitmapFactory.decodeByteArray(imgs, 0, len);
            }

看到这个https://stackoverflow.com/questions/9885154/how-to-get-images-from-xmpp-in-android-application/10272947#10272947

于 2012-07-26T11:35:37.353 回答