title: Integrate Facebook chat in android with Asmack API
body: <p>I try to implement chat support as suggested in <a href="http://davanum.wordpress.com/2008/12/29/updated-xmpp-client-for-android/" rel="nofollow">this blog</a>.</p>
  
  <p>Currently, I'm using following code to implement chatting interface.</p>
  
  <p><em><strong>Java Code</em></strong></p>
  
  <p><strong>TestChatActivity.java</strong></p>
  
  <pre><code>import java.util.ArrayList;
  import org.jivesoftware.smack.ConnectionConfiguration;
  import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
  import org.jivesoftware.smack.PacketListener;
  import org.jivesoftware.smack.XMPPConnection;
  import org.jivesoftware.smack.XMPPException;
  import org.jivesoftware.smack.filter.MessageTypeFilter;
  import org.jivesoftware.smack.filter.PacketFilter;
  import org.jivesoftware.smack.packet.Message;
  import org.jivesoftware.smack.packet.Packet;
  import org.jivesoftware.smack.packet.Presence;
  import org.jivesoftware.smack.util.StringUtils;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  
  public class TestChatActivity extends Activity implements OnClickListener {
  
  private ArrayList&lt;String&gt; messages = new ArrayList();
  private Handler mHandler = new Handler();
  private EditText mRecipient;
  private EditText mSendText;
  private ListView mList;
  private XMPPConnection mConnection;
  private String mHost, mPort, mService, mUsername, mPassword;
  private ConnectionConfiguration mConnConfig;
  private String TAG;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      initLayout();
      // initGtalk();
      initFB();
  
      // Create a connection
      createConnection();
  
      // login
      loginToXMPP();
  
  }
  
  void initLayout() {
      Log.i("XMPPClient", "onCreate called");
      setContentView(R.layout.main);
  
      mRecipient = (EditText) this.findViewById(R.id.recipient);
      Log.i("XMPPClient", "mRecipient = " + mRecipient);
      mSendText = (EditText) this.findViewById(R.id.sendText);
      Log.i("XMPPClient", "mSendText = " + mSendText);
      mList = (ListView) this.findViewById(R.id.listMessages);
      Log.i("XMPPClient", "mList = " + mList);
      // Set a listener to send a chat text message
      Button send = (Button) this.findViewById(R.id.send);
      send.setOnClickListener(this);
  
      setListAdapter();
  }
  
  void initGtalk() {
      mHost = "talk.google.com";
      mPort = "5222";
      mService = "gmail";
      mUsername = "abc@gmail.com";
      mPassword = "password";
      // Set Default recipients for Gtalk
      mRecipient.setText("default_buddy@gmail.com");
  }
  
  void initFB() {
      mHost = "chat.facebook.com";
      mPort = "5222";
      mService = "xmpp";
      mUsername = "userid@chat.facebook.com";
      mPassword = "password";
      // Set Default recipients for FB
      mRecipient.setText("new_userid@chat.facebook.com");
  }
  
  void createConnection() {
      mConnConfig = new ConnectionConfiguration(mHost,
              Integer.parseInt(mPort), mService);
      mConnConfig.setSecurityMode(SecurityMode.required);
      mConnConfig.setSASLAuthenticationEnabled(true);
      mConnection = new XMPPConnection(mConnConfig);
  
      try {
          mConnection.connect();
          Log.i("XMPPClient",
                  "[SettingsDialog] Connected to " + mConnection.getHost());
      } catch (XMPPException ex) {
          Log.e("XMPPClient", "[SettingsDialog] Failed to connect to "
                  + mConnection.getHost());
          Log.e("XMPPClient", ex.toString());
          setConnection(null);
      }
  }
  
  void loginToXMPP() {
      try {
          mConnection.login(mUsername, mPassword);
          Log.i("XMPPClient", "Logged in as " + mConnection.getUser());
  
          // Set the status to available
          Presence presence = new Presence(Presence.Type.available);
          mConnection.sendPacket(presence);
          setConnection(mConnection);
      } catch (XMPPException ex) {
          Log.e("XMPPClient", "[SettingsDialog] Failed to log in as "
                  + mUsername);
          Log.e("XMPPClient", ex.toString());
          setConnection(null);
      }
  }
  
  /**
   * Called by Settings dialog when a connection is established with the XMPP
   * server
   * 
   * @param connection
   */
  public void setConnection(XMPPConnection connection) {
      this.mConnection = connection;
      if (connection != null) {
          // Add a packet listener to get messages sent to us
          PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
          connection.addPacketListener(new PacketListener() {
              public void processPacket(Packet packet) {
                  Message message = (Message) packet;
                  if (message.getBody() != null) {
                      String fromName = StringUtils.parseBareAddress(message
                              .getFrom());
                      Log.i("XMPPClient", "Got text [" + message.getBody()
                              + "] from [" + fromName + "]");
                      messages.add(fromName + ":");
                      messages.add(message.getBody());
                      // Add the incoming message to the list view
                      mHandler.post(new Runnable() {
                          public void run() {
                              setListAdapter();
                          }
                      });
                  }
              }
          }, filter);
      }
  }
  
  private void setListAdapter() {
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
              R.layout.multi_line_list_item, messages);
      mList.setAdapter(adapter);
  }
  
  public void onClick(View v) {
      if (v.getId() == R.id.send) {
          String to = mRecipient.getText().toString();
          String text = mSendText.getText().toString();
  
          Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
          Message msg = new Message(to, Message.Type.chat);
          msg.setBody(text);
          mConnection.sendPacket(msg);
          messages.add(mConnection.getUser() + ":");
          messages.add(text);
          setListAdapter();
      }
  
  }
  }
  </code></pre>
  
  <p><strong>Layouts</strong> </p>
  
  <p><strong>main.xml</strong></p>
  
  <p>
  </p>
  
  <pre><code>&lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" &gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:minWidth="70dp"
          android:text="Recipient:" /&gt;
  
      &lt;EditText
          android:id="@+id/recipient"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:autoText="false"
          android:capitalize="none"
          android:minWidth="250dp"
          android:scrollHorizontally="true"
          android:singleLine="true"
          android:textSize="16sp" /&gt;
  &lt;/LinearLayout&gt;
  
  &lt;ListView
      android:id="@+id/listMessages"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:scrollbars="horizontal" /&gt;
  
  &lt;EditText
      android:id="@+id/sendText"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:autoText="false"
      android:capitalize="none"
      android:scrollHorizontally="true"
      android:singleLine="true"
      android:textSize="16sp" /&gt;
  
  &lt;Button
      android:id="@+id/send"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Send" &gt;
  &lt;/Button&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>multi_line_list_item.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/text1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:singleLine="false"
  android:textStyle="bold" /&gt;
  </code></pre>
  
  <p>Above code uses <strong>Asmack</strong> Library  </p>
  
  <p>Google Talk work fine for me but Facebook chat not worked with this code. :(</p>
  
  <p>Is there any special configuration used for Facebook chat connectivity? Or this method wrong to connect with Facebook chat?</p>
  
  <p>Because I’ve done many R&amp;D on that and Facebook support XMPP chat support as discussed in <a href="https://www.facebook.com/sitetour/chat.php" rel="nofollow">Facebook XMPP chat Settings</a> so, As I ‘am thinking this should be work.</p>
  
  <p>Anybody have implemented Facebook chat in android can help me to resolve this problem.</p>
  
  <p>Thanks in advance...!!!</p>