Thursday, December 11, 2014

Android XMPP login

To register in XMPP server first of all you need to CREATE XMPP CONNECTION

After connection to XMPP server. If you don't have registration in XMPP server, for Registration click here

For login  login("USERNAME", "PASSWORD") method is used.  In this method you need to pass username and password as parameter.

For this example below is the code for xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn_Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_centerInParent="true"
        android:layout_marginTop="38dp"
        android:text="Login" />

</RelativeLayout>


Use below activity for the login to XMPP server.

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
 
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class TestActivity extends Activity{
 
 private Button btn_Login;
 
 XMPPConnection connection ;
 ConnectionConfiguration config;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.demo_activity);
  
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
      .detectAll()
      .penaltyLog()
      .build();
  StrictMode.setThreadPolicy(policy);
 
  
  btn_Login = (Button) findViewById(R.id.btn_Login);
  
  btn_Login.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View v) {
    try {
     try {
      if (connection.isConnected()) {
       connection.disconnect();
       connection.connect();
       connection.login("USERNAME", "PASSWORD");
       
      }else {
       connection.connect();
       connection.login("USERNAME", "PASSWORD");
       
      }
      
     } catch (XMPPException e) {
      e.printStackTrace();
     }
     
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
}

Hope this post may helpful for you...

No comments:

Post a Comment