Thursday, 8 September 2011

XMPP or Jabber for Chatting in Android

This Tutorial will tell you how to create a XMPP Client  for Android.
You can connect to any Server  like Gmail or Facebook or create your own Server using Open Fire.

  • For Information regarding how to setup Jabber Messaging Server and Use Miranda IM Client refer to:
             http://www.wowtutorial.org/tutorial/52.html


  • And download Miranda IM Client 
  • Next thing is to Connect to this Server Using your own Android Application: 
          You need to use the Smack Api for this purpose, which can be downloaded from the follwing link:
OR
 
After You have downlaoded the Smack Library Now you need to add it in your project's Refrenced Libraries. 
 
       1.Connection With The Server:
 
private XMPPClient xmppClient;
    private XMPPConnection connection;
    private ArrayList<String> messages = new ArrayList(); 
    public void login(String userName, String password) throws XMPPException
    {
    ConnectionConfiguration config = new ConnectionConfiguration("HOST",PORT, "SERVICE");
    connection = new XMPPConnection(config);
    connection.connect();
 
    //LOGIN USING THE FOLLWING login() 
 
    connection.login(userName, password);
    xmppClient.setConnection(connection); 
    }
 
  2.Send Message to a Person:
 
   Message msg = new Message("Recepiont", Message.Type.chat);
 msg.setBody(text);
 connection.sendPacket(msg); 

  3.For incoming messages from other users:
 
  public void setConnection
 (XMPPConnection connection) {
 this.connection = 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);
  }
 }
 
  4.To Sign Out or Disconnect from the Server: 

 public void signOut()
 {
  connection.disconnect();
 }
 
 
 
  There are a lot of other things which you can do using smack API.
  You can find the detailed documentation of each class in the following link:
http://www.igniterealtime.org/builds/smack/docs/latest/documentation/index.html






SOURCE: http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/




 

9 comments:

  1. welcome iintrance..For any other queries please feel free to communicate

    ReplyDelete
  2. Hi Karan,
    Iam faing problem in add contact ,I am using xmpp+smack api.
    I am sending request to contact and after recieving packet ,i want to accept new request on a button click but no idea how to manage it after recieving packet .

    ReplyDelete
  3. Try the following code:

    /**
    * New Friend can be Added using
    * the Roster Class
    */
    public void addFriend()
    {
    Roster roster=connection.getRoster();
    try {
    roster.createEntry("User","Name",null);

    } catch (XMPPException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    ReplyDelete
  4. Hey Karan..In my application ,there is a chat application only for application users.And before this I never implemented chat .I have no idea how to implement chat client and what are the parts of the server side..So can you advice me about how to do it..or you can give me the tutorial link for basic chat application..Thanx in andvance

    ReplyDelete
  5. Hi Karan, this is helpfull information. iam new to android and asmack(smack for android) api. iam facing problem to get the presence status of friends. ofcourse asmack defines corresponding classes to do this but does asmack support this feature. if possible please explain me with source code. thank you

    ReplyDelete
  6. Does the smack api works on Android ? I'm using it and i can't connect to Openfire server and the error i'm getting seems to have to do with the fact that smack doesn't work with android and i have to use aSmack which is a third part version of smack to use with android

    ReplyDelete
  7. Hi Karan,

    Can you help me with XMPP as it is totally new for me and i want to know how to start with basic chat application in android.

    ReplyDelete
  8. I want to thank you for writing this article.This is great Article for me. It also more very informative & awesome. I expect more articles from you in future.
    XMPP

    ReplyDelete