Facebook asmack xmpp client returns random numbers for roster

元气小坏坏 提交于 2020-01-01 07:06:06

问题


I followed the guidlines given in the following SO question to connect to facebook chat using xmpp and I am able to connect to facebook and pull the correct number of contacts but when it prints the contacts they are all random numbers @chat.facebook.com and all return offline.

Android Facebook chat example project

public void connectToFb() throws XMPPException {

        ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
        config.setSASLAuthenticationEnabled(true);
        config.setSecurityMode(SecurityMode.required);
        config.setRosterLoadedAtLogin(true);
        config.setTruststorePath("/system/etc/security/cacerts.bks");
        config.setTruststorePassword("changeit");
        config.setTruststoreType("bks");
        config.setSendPresence(false);
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, MemorizingTrustManager.getInstanceList(this), new java.security.SecureRandom());
            config.setCustomSSLContext(sc);
        } catch (GeneralSecurityException e) {
            Log.w("TAG", "Unable to use MemorizingTrustManager", e);
        }
        XMPPConnection xmpp = new XMPPConnection(config);
        try {
            xmpp.connect();
            xmpp.login("user.name", "password"); // Here you have to used only facebookusername from facebookusername@chat.facebook.com
            Roster roster = xmpp.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            System.out.println("Connected!");
            System.out.println("\n\n" + entries.size() + " buddy(ies):");
            // shows first time onliners---->
            String temp[] = new String[50];
            int i = 0;
            for (RosterEntry entry : entries) {
                String user = entry.getUser();
                Log.i("TAG", user);
            }
        } catch (XMPPException e) {
            xmpp.disconnect();
            e.printStackTrace();
        }
        }

回答1:


It sounds like you just want to readable name, so try using

rosterEntry.getName()

which returns the users name, instead of

rosterEntry.getUser()

which returns the JID.

Not sure about your offline problem though. How are you checking? You have to set up a roster listener to get changes in presence.




回答2:


Its a bug in XMPP library . There is a work around for that .

Step 1: Connect to XMPP.

Step 2: Login to facebook account through xmpp.

Step 3: get online friend list using this fql query .

    SELECT uid, name, online_presence ,
      sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Then i concat address with string uid@chat.facebook.com and communicate via XMPP.




回答3:


(It's not 100% clear what problem you're having regarding the online/offline thing may be a bug or something you're doing incorrectly), but you won't get back the user's actual user IDs in the response, this is mentioned in the docs:

The user's own Jabber ID (JID) is different from the Jabber ID that their contacts will see because the translation is done internally.



来源:https://stackoverflow.com/questions/13079632/facebook-asmack-xmpp-client-returns-random-numbers-for-roster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!