Wednesday, 9 September 2015

Registration And Login Using With JSON

Activity_Register.xml

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/firstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:ems="10"
        android:hint="FirstName"
        android:inputType="textPersonName"
        
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/lastName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/firstName"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="LastName"
        android:inputType="textPersonName"
        
        android:singleLine="true" />

    <EditText
        android:id="@+id/emailID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lastName"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:hint="Emailid"
        android:inputType="textEmailAddress"
        android:singleLine="true" />

    <EditText
        android:id="@+id/mobileNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/emailID"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:hint="Mobile"
        android:inputType="number"
        android:singleLine="true" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mobileNumber"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textVisiblePassword"
        android:singleLine="true" />

    <Button
        android:id="@+id/register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:text="Register" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:text="Login" />

</RelativeLayout>

Acrivity_login.xml

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/emailID_Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:hint="Emailid"
        android:inputType="textEmailAddress"
        android:singleLine="true" />

    <EditText
        android:id="@+id/password_Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/emailID_Login"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textVisiblePassword"
        android:singleLine="true" />

    <Button
        android:id="@+id/loGin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password_Login"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:text="Login" />

</RelativeLayout>

Activity_LoginPage.xml

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/logout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="108dp"
        android:text="Logout" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="106dp"
        android:text="Login Successfully"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="Welcome"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/userId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:textSize="15sp"
        android:text="" />

</RelativeLayout>

RegisterActivity.java

package com.example.logindemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {

private EditText firstName,lastName,emailID,mobileNumber,password;
private Button register, login;
private String result="",url="",outResult="";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

firstName = (EditText)findViewById(R.id.firstName);
lastName = (EditText)findViewById(R.id.lastName);
emailID = (EditText)findViewById(R.id.emailID);
mobileNumber = (EditText)findViewById(R.id.mobileNumber);
password = (EditText)findViewById(R.id.password);
register = (Button)findViewById(R.id.register);
login=(Button) findViewById(R.id.button1);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(i);
}
});
register.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
String fn = firstName.getText().toString();
String ln = lastName.getText().toString();
String ed = emailID.getText().toString();
String m = mobileNumber.getText().toString();
String p = password.getText().toString();
registerLogin rl = new registerLogin();
AsyncTask<String, String, String> outPut = rl.execute(fn,ln,ed,m,p);
try {
String registerResult = outPut.get();
System.out.println("The Result is"+registerResult);
if(registerResult.equals("Success")){
Intent moveToLogin = new Intent(RegisterActivity.this,LoginActivity.class);
moveToLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(moveToLogin);
RegisterActivity.this.finish();
Toast.makeText(getApplicationContext(), "Register Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Register Unsuccessfully", Toast.LENGTH_SHORT).show();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public class registerLogin extends AsyncTask<String,String,String>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
System.out.println("In background....." + params[0] + "--"+ params[1] + "--" + params[2]+ "--" + params[3]+ "--" + params[4]);
url = "http://www.biomedeq.com/android_check/register.php?firstname=" + params[0] + "&lastname=" + params[1] + "&emailid=" + params[2] + "&mobile=" + params[3] + "&password=" + params[4];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
System.out.println(url);
HttpResponse response;

try {
response = httpclient.execute(httppost);
InputStream is;
is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
outResult = br.readLine();
System.out.print(outResult.toString());

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outResult;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
}


LoginActivity.java

package com.example.logindemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.logindemo.RegisterActivity.registerLogin;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
private String result="",url="";
private EditText email,password;
private Button login;

@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
email = (EditText)findViewById(R.id.emailID_Login);
password = (EditText)findViewById(R.id.password_Login);
login = (Button)findViewById(R.id.loGin);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String eid = email.getText().toString();
String pass = password.getText().toString();

LoginCheck lc = new LoginCheck();
AsyncTask<String, String, String> outPut = lc.execute(eid,pass);
try {
String loginResult = outPut.get();
System.out.println("The Result is"+loginResult);
if(result.equals("Success")){
Intent nextPage = new Intent(LoginActivity.this,LoginPageActivity.class);
nextPage.putExtra("user", email.getText().toString());
nextPage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(nextPage);
LoginActivity.this.finish();
Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "Login Unsuccessfully", Toast.LENGTH_SHORT).show();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public class LoginCheck extends AsyncTask<String,String,String>{

@Override
protected String doInBackground(String... params) {
System.out.println("In background....." + params[0] + "--"+ params[1]);
url = "http://www.biomedeq.com/android_check/login.php?&emailid=" + params[0] + "&password=" + params[1];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
System.out.println(url);
HttpResponse response;

try {
response = httpclient.execute(httppost);
InputStream is;
is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
result = br.readLine();
System.out.print(result.toString());

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
}

LoginPageActivity.java

package com.example.logindemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class LoginPageActivity extends Activity {
private Button logout;
private TextView user;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginpage);
logout = (Button)findViewById(R.id.logout);
user = (TextView) findViewById(R.id.userId);
String userName = getIntent().getStringExtra("user");
user.setText(userName);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

LoginPageActivity.this.finish();
}
});
}
}

Android Manifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.logindemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".RegisterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login" >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
       
        <activity
            android:name=".LoginPageActivity"
            android:label="@string/title_activity_login_page" >
        </activity>
    </application>

</manifest>

No comments:

Post a Comment

Ads Inside Post