Download Code

Android Login Register JSON Php Mysql

In android login register json php mysql tutorial you will learn how to develop Android Login and Registration with PHP,MYSQL using JSONParser and HTTP Components.Android Login and Registration is mostly used for Commerce Apps.Android Login can be used for any apps which require user session management.

Before starting this topic you must have some basic knowledge of PHP and MYSQL.You can learn these topics from W3Schools.com or tutorialspoint.com.

You should configure your system according to following steps.

  • Install XAMPP SERVER.
  • You must have internet connection because you will work on Android Phone.

What you will use in android Login Register JSON PHP MYSQL tutorial.

Create new mysql database and table on XAMPP Server.

JSON,JSON Parser,JSON Parser in android,JSON Parser in android application,http,httpclient,httpclient in android,httppost,httpget,httpget in android,httppost in android, login system in android,android login system,how to develop login system in android,sign in sign up in android application,asynctask,asynctask in android, asynctask in android application,textview in android,textview,button,button click,toast,JSONObject,JSONObject in android,JSONArray,JSONArray in android, getJSONObject,getJSONObject in android,getString in android,how to parse data in android,login system in android using php and mysql,login system with php and mysql in android, android server end code of php for sign in and sign up,android server end code of php for login system,progressdialog,progressdialog in android,onpreExecute,onPostExecute,doInbackground, How to check internet connection,how to check internet connection in android,android login register json php mysql,android login register,

Create new android login register json php mysql application project.Design two layouts.

  • LogIn Layout (activity_main.xml)
  • Sign Up Layout (sign_up.xml)

JSON,JSON Parser,JSON Parser in android,JSON Parser in android application,http,httpclient,httpclient in android,httppost,httpget,httpget in android,httppost in android, login system in android,android login system,how to develop login system in android,sign in sign up in android application,asynctask,asynctask in android, asynctask in android application,textview in android,textview,button,button click,toast,JSONObject,JSONObject in android,JSONArray,JSONArray in android, getJSONObject,getJSONObject in android,getString in android,how to parse data in android,login system in android using php and mysql,login system with php and mysql in android, android server end code of php for sign in and sign up,android server end code of php for login system,progressdialog,progressdialog in android,onpreExecute,onPostExecute,doInbackground, How to check internet connection,how to check internet connection in android,android login register json php mysql,android login register,

activity_main.xml(LogIn)

JSON,JSON Parser,JSON Parser in android,JSON Parser in android application,http,httpclient,httpclient in android,httppost,httpget,httpget in android,httppost in android, login system in android,android login system,how to develop login system in android,sign in sign up in android application,asynctask,asynctask in android, asynctask in android application,textview in android,textview,button,button click,toast,JSONObject,JSONObject in android,JSONArray,JSONArray in android, getJSONObject,getJSONObject in android,getString in android,how to parse data in android,login system in android using php and mysql,login system with php and mysql in android, android server end code of php for sign in and sign up,android server end code of php for login system,progressdialog,progressdialog in android,onpreExecute,onPostExecute,doInbackground, How to check internet connection,how to check internet connection in android,android login register json php mysql,android login register,

sign_up.xml

JSON,JSON Parser,JSON Parser in android,JSON Parser in android application,http,httpclient,httpclient in android,httppost,httpget,httpget in android,httppost in android, login system in android,android login system,how to develop login system in android,sign in sign up in android application,asynctask,asynctask in android, asynctask in android application,textview in android,textview,button,button click,toast,JSONObject,JSONObject in android,JSONArray,JSONArray in android, getJSONObject,getJSONObject in android,getString in android,how to parse data in android,login system in android using php and mysql,login system with php and mysql in android, android server end code of php for sign in and sign up,android server end code of php for login system,progressdialog,progressdialog in android,onpreExecute,onPostExecute,doInbackground, How to check internet connection,how to check internet connection in android,android login register json php mysql,android login register,

Create a class JSONParser within android Login Register JSON PHP MYSQL project which will parse the data.

JSONParser.java

package developer.codeclogin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

	static InputStream is = null;
	static JSONObject jObj = null;
	static String json = "";

	// constructor
	public JSONParser() {

	}
	public JSONObject getJSONFromUrl(String url, List params) {

		// Making HTTP request
		try {
			// check for request method
			
				// defaultHttpClient
				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpPost httpPost = new HttpPost(url);
				httpPost.setEntity(new UrlEncodedFormEntity(params));

				HttpResponse httpResponse = httpClient.execute(httpPost);
				HttpEntity httpEntity = httpResponse.getEntity();
				is = httpEntity.getContent();
			

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			json = sb.toString();
			Log.e("JSON", json);
		} catch (Exception e) {
			Log.e("Buffer Error", "Error converting result " + e.toString());
		}
		// try parse the string to a JSON object
		try {
			jObj = new JSONObject(json);
		} catch (JSONException e) {
			Log.e("JSON Parser", "Error parsing data " + e.toString());
		}
		// return JSON String
		return jObj;
	}
}

Create another class for Sign In and Sign Up methods.This class contains server url and tags.The ip in the give class is a wifi IPv4.If you want to work on localhost just change it to “http://localhost/Login/”.If you still want to work on Android phone.Go to CMD > ipconfig > IPv4.Connect both system(Android,PC) with one internet connection when dealing with local server.

userMethods.java

package developer.codeclogin;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class userMethods {
	private JSONParser jsonParser; 	    // object of JSONParser class
	private static String SERVER_URL="http://your_server_ip/Login/";     // mysql database server url string
	private static String SIGN_IN_TAG="signIn";  // tag to detect for signing in the user
	private static String SIGN_UP_TAG="signUp";   // tag to detect signing up the new user

	// constructor of user method class to initialize JSONParser
	public userMethods(){
		jsonParser = new JSONParser();
	}
	// method to login User with email and password
	public JSONObject signInUser(String email, String password){
		// Building Parameters
		try {
			List params = new ArrayList();
			params.add(new BasicNameValuePair("tag", SIGN_IN_TAG));
			params.add(new BasicNameValuePair("email", email));
			params.add(new BasicNameValuePair("password", password));
                        //passing mysql database url with values
			JSONObject json = jsonParser.getJSONFromUrl(SERVER_URL, params);
			return json;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	// method to signUp new user
	public JSONObject signUpUser(String fname, String email, String pass){
		// Building Parameters
		List params = new ArrayList();
		params.add(new BasicNameValuePair("tag", SIGN_UP_TAG));
		params.add(new BasicNameValuePair("name", fname));
		params.add(new BasicNameValuePair("useremail", email));
		params.add(new BasicNameValuePair("userpassword", pass));

		JSONObject json = jsonParser.getJSONFromUrl(SERVER_URL,params);
		return json;
	}
}

Create a class for Sign Up new user.

signUpUser.java

package developer.codeclogin;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class signUpUser extends Activity implements OnClickListener{

	EditText userName,userEmail,userPassword;
	Button signUp;

	// database parameters
	private static String KEY_SUCCESS = "success";
	private static String KEY_ERROR = "error";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_up);
		initialize();
	}
	public void initialize(){
		userName = (EditText) findViewById(R.id.newUserName);
		userEmail = (EditText) findViewById(R.id.newUserEmail);
		userPassword = (EditText) findViewById(R.id.newUserPassword);
		signUp = (Button) findViewById(R.id.signUpBtn);
		signUp.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.signUpBtn:
			NetAsync(v);
			break;
		}
	}

	// Asychronous Tasks

	/**
	 * Async Task to check whether internet connection is working
	 **/

	private class NetCheck extends AsyncTask<String,String,Boolean>
	{
		private ProgressDialog nDialog;

		@Override
		protected void onPreExecute(){
			super.onPreExecute();
			nDialog = new ProgressDialog(signUpUser.this);
			nDialog.setMessage("Loading..");
			nDialog.setTitle("Checking Network");
			nDialog.setIndeterminate(false);
			nDialog.setCancelable(true);
			nDialog.show();
		}

		@Override
		protected Boolean doInBackground(String... args){


			/**
			 * Gets current device state and checks for working internet connection by trying Google.
			 **/
			ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo netInfo = cm.getActiveNetworkInfo();
			if (netInfo != null && netInfo.isConnected()) {
				try {
					URL url = new URL("http://www.google.com");
					HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
					urlc.setConnectTimeout(3000);
					urlc.connect();
					if (urlc.getResponseCode() == 200) {
						return true;
					}
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return false;

		}
		@Override
		protected void onPostExecute(Boolean th){

			if(th == true){
				nDialog.dismiss();
				new ProcessRegister().execute();
			}
			else{
				nDialog.dismiss();

			}
		}
	}

	private class ProcessRegister extends AsyncTask<String, String, JSONObject> {

		/**
		 * Defining Process dialog
		 **/
		private ProgressDialog pDialog;


		String fullname,email,password;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			fullname = userName.getText().toString();
			email = userEmail.getText().toString();
			password = userPassword.getText().toString();
			pDialog = new ProgressDialog(signUpUser.this);
			pDialog.setTitle("Contacting Servers");
			pDialog.setMessage("Registering ...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected JSONObject doInBackground(String... args) {

			userMethods userMethod = new userMethods();

			JSONObject json = userMethod.signUpUser(fullname, email, password);

			return json;


		}
		@Override
		protected void onPostExecute(JSONObject json) {
			/**
			 * Checks for success message.
			 **/
			try {
				if (json.getString(KEY_SUCCESS) != null) {
					String res = json.getString(KEY_SUCCESS);
					Log.e("check First", res);
					String red = json.getString(KEY_ERROR);
					Log.e("check Second", red);
					if(Integer.parseInt(res) == 1){
						pDialog.dismiss();
						Toast.makeText(getApplicationContext(), "User Registered Successfully", Toast.LENGTH_SHORT).show();
					}
					else if (Integer.parseInt(red) == 2){
						pDialog.dismiss();
						Toast.makeText(getApplicationContext(), "User Not Registered Successfully", Toast.LENGTH_SHORT).show();
					}
				}
				else{
					pDialog.dismiss();
				}

			} catch (JSONException e) {
				e.printStackTrace();


			}
		}
	}
	public void NetAsync(View view){
		new NetCheck().execute();
	}

}

Now go to MainActivity.java class which is used as Android Login user.

MainActivity.java

package developer.codeclogin;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;


import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {

	TextView signUp;
	EditText email,password;
	Button signIn;

	// database parameters
	private static String KEY_SUCCESS = "success";
	private static String KEY_ERROR = "error";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialize();
	}
	public void initialize(){
		signUp = (TextView) findViewById(R.id.signUpTxt);
		email = (EditText) findViewById(R.id.userEmail);
		password = (EditText) findViewById(R.id.userPass);
		signIn = (Button) findViewById(R.id.signInBtn);
		signUp.setOnClickListener(this);
		signIn.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.signInBtn:
			NetAsync(v);
			break;
		case R.id.signUpTxt:
			Intent u = new Intent(getApplicationContext(),signUpUser.class);
			startActivity(u);
			finish();
			break;
		}
	}
	// Asychronous Tasks

		/**
		 * Async Task to check whether internet connection is working
		 **/

		private class NetCheck extends AsyncTask<String,String,Boolean>
		{
			private ProgressDialog nDialog;

			@Override
			protected void onPreExecute(){
				super.onPreExecute();
				nDialog = new ProgressDialog(MainActivity.this);
				nDialog.setMessage("Loading..");
				nDialog.setTitle("Checking Network");
				nDialog.setIndeterminate(false);
				nDialog.setCancelable(true);
				nDialog.show();
			}

			@Override
			protected Boolean doInBackground(String... args){


				/**
				 * Gets current device state and checks for working internet connection by trying Google.
				 **/
				ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo netInfo = cm.getActiveNetworkInfo();
				if (netInfo != null && netInfo.isConnected()) {
					try {
						URL url = new URL("http://www.google.com");
						HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
						urlc.setConnectTimeout(3000);
						urlc.connect();
						if (urlc.getResponseCode() == 200) {
							return true;
						}
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return false;

			}
			@Override
			protected void onPostExecute(Boolean th){

				if(th == true){
					nDialog.dismiss();
					new ProcessRegister().execute();
				}
				else{
					nDialog.dismiss();

				}
			}
		}

		private class ProcessRegister extends AsyncTask<String, String, JSONObject> {

			/**
			 * Defining Process dialog
			 **/
			private ProgressDialog pDialog;


			String inEmail,inPassword;
			@Override
			protected void onPreExecute() {
				super.onPreExecute();

				
				inEmail = email.getText().toString();
				inPassword = password.getText().toString();
				pDialog = new ProgressDialog(MainActivity.this);
				pDialog.setTitle("Contacting Servers");
				pDialog.setMessage("Registering ...");
				pDialog.setIndeterminate(false);
				pDialog.setCancelable(true);
				pDialog.show();
			}

			@Override
			protected JSONObject doInBackground(String... args) {

				userMethods userMethod = new userMethods();

				JSONObject json = userMethod.signInUser(inEmail, inPassword);

				return json;


			}
			@Override
			protected void onPostExecute(JSONObject json) {
				/**
				 * Checks for success message.
				 **/
				try {
					if (json.getString(KEY_SUCCESS) != null) {
						String res = json.getString(KEY_SUCCESS);
						Log.e("check First", res);
						String red = json.getString(KEY_ERROR);
						Log.e("check Second", red);
						if(Integer.parseInt(res) == 1){
							pDialog.dismiss();
							Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_SHORT).show();
						}
						else if (Integer.parseInt(red) == 2){
							pDialog.dismiss();
							Toast.makeText(getApplicationContext(), "Not Login Successfully", Toast.LENGTH_SHORT).show();
						}
					}
					else{
						pDialog.dismiss();
					}

				} catch (JSONException e) {
					e.printStackTrace();


				}
			}
		}
		public void NetAsync(View view){
			new NetCheck().execute();
		}


}

At last Add permissions in android Login Register JSON PHP MYSQL project Android Manifest.xml file.

JSON,JSON Parser,JSON Parser in android,JSON Parser in android application,http,httpclient,httpclient in android,httppost,httpget,httpget in android,httppost in android, login system in android,android login system,how to develop login system in android,sign in sign up in android application,asynctask,asynctask in android, asynctask in android application,textview in android,textview,button,button click,toast,JSONObject,JSONObject in android,JSONArray,JSONArray in android, getJSONObject,getJSONObject in android,getString in android,how to parse data in android,login system in android using php and mysql,login system with php and mysql in android, android server end code of php for sign in and sign up,android server end code of php for login system,progressdialog,progressdialog in android,onpreExecute,onPostExecute,doInbackground, How to check internet connection,how to check internet connection in android,android login register json php mysql,android login register,

MYSQL Database Server End is Coded in php File.Download Code and Find index.php and place that folder in XAMPP htdocs folder.

Now run your android Login Register JSON PHP MYSQL application project.
That’s it for this android Login Register JSON PHP MYSQL tutorial.
Thanks.