3

Before anybody even cries duplication, I'd like to say that none of the questions currently posted here in StackOverflow got me started.

I'm currently tasked to create a simple Facebook chat client for Android. Basically, I just log in and get to see who my online contacts are, then, I send and/or receive messages. I've never done anything like this before so I went on to study XMPP and even attempted to create an app in http://developers.facebook.com/apps so I can get an API key for all the authentication which I think I need to undergo. But after five excruciating days of researching on XMPP and asmack and on the Graph and Chat API, I was told I didn't need to study all those and the SASLAuthentication classes and that I didn't need to get an API key. All I had to do was use the X-FACEBOOK-PLATFORM mechanism (whose every related document in the web I've already read) and send messages directly to username@chat.facebook.com. But there's not a single good and complete guide on how it's coded in Java, or Android for that matter.

Help me get started? And how does asmack have anything to do with this? I totally don't get it how I need to use asmack and not need SASLAuthentication. Thanks.

Robin
  • 24,062
  • 5
  • 49
  • 58
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
  • Well I guess Facebook has already launched an offical chat app for Android and iPhone. See http://blog.facebook.com/blog.php?post=10150249543542131 – AppleGrew Aug 16 '11 at 10:40
  • I know, I'm tasked to do it anyway for my sadistic boss's viewing pleasure. – Matthew Quiros Aug 16 '11 at 10:41
  • Please check this answer http://stackoverflow.com/a/11238124/1472665, It may help you to create new Facebook Chat Client – Dipali Jun 28 '12 at 10:30

3 Answers3

1

you can use XmppConnection to connect with the server..and then fetch online friends using roster.. here is some code to connect, login and fetch friends and use this code in async task not in main ui thread..

ConnectionConfiguration connConfig = new ConnectionConfiguration("chat.facebook.com", 5222 , "chat.facebook.com");
    connConfig.setSASLAuthenticationEnabled(true);
    connConfig.setSecurityMode(SecurityMode.required);
    connConfig.setRosterLoadedAtLogin(true);
    connConfig.setSendPresence(false);
    connection = new XMPPConnection(connConfig);

    try 
    {
        connection.connect();
        Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
    }
    catch (XMPPException ex) 
    {
        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());

        XMPPLogic.getInstance().setConnection(null);
    }

    try 
    {
        connection.login(username, password);
        try 
        {
            Thread.sleep(time);
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("XMPPClient", "Logged in as " + connection.getUser());

        // Set the status to available
        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);

        XMPPLogic.getInstance().setConnection(connection);
        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
        ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp", new VCardProvider());

        Log.i("Roster", "Roster Connected");
        Log.i("Entries", "\n\n" + entries.size() + " buddy(ies):");

        for (RosterEntry entry : entries) 
        {

            if (roster.getPresence(entry.getUser()).isAvailable()) 
            {


            HashMap<String, Object> contact = new HashMap<String, Object>();
            contact.put(NAME, entry.getName());
            contact.put(USERID, entry.getUser());
            datatList.add(contact);

            Log.i("TAG", entry.getName() + entry.getUser());
            Log.i("Contact", contact.toString());

            }

        }



    }
    catch (XMPPException ex) 
    {
        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);

        XMPPLogic.getInstance().setConnection(null);
    }
andrammer
  • 33
  • 7
1

Just a bit of information.

  • asmack is a fork of Smack designed specifically to enable Smack on the Android platform.
  • Smack is a client library for XMPP, which as you know is how you can chat with FB users.
  • SASLAuthentication is simply an authentication means used within XMPP to login to the XMPP server. In your case this server is the FB one.

There is a discussion thread at Ignite Realtime on connecting to FB with Smack (Several in fact if you do a search).

As a side note, I believe I read somewhere that asmack is no longer being maintained/developed. I think most developers are simply making a few tweaks to the Smack source to make it work on Android themselves.

Robin
  • 24,062
  • 5
  • 49
  • 58
0

Sad to hear about your boss. Don't try to understand XMPP. FB has its own APIs for chat. Read http://developers.facebook.com/docs/chat/

AppleGrew
  • 9,302
  • 24
  • 80
  • 124