my previous article Android Login and Registration Screen Design i explained designing the login and registration interfaces, but it has no functionality. In this tutorial i am explaining how to build complete login and registration system in android using PHP, MySQL and SQLite. Also this tutorial covers how to build simple API using PHP and MySQL.
This tutorial is combination of some of my previous tutorials. I hope you covered these tutorials before.
Android making HTTP Requests
Android JSON Parsing Tutorial
Android SQLite Database Tutorial
Android Login and Registration Screen Design
API (Application Programming Interface)
⇒ Accepting requests by GET/POST methods
⇒ Interact with PHP classes to get data from database or store in database
⇒ Finally will give output in JSON format
1. Creating MySQL Database and Tables
As I am writing API in PHP I selected MySql database to maintain users and other related information. Open your mysql console or phpmyadmin and run following query to create database and users table.
2. Building PHP API Classes
To make it minimum i tried to use less number of php files. Following are the files are required to build API in php. You can find description of each file in the below image.
config.php – This file contains constant variables to connect to database.
DB_Connect.php – This file is used to connect or disconnect to database.
DB_Functions.php – This file contains functions to store user in database, get user from database. You can also add methods like update user, delete user.
user unique id – I am generating unique user id in php using uniqid(”, true) function. Sample user id will be like 4f074eca601fb8.88015924
Encrypted Password – This password is stored using base64_encode method. Each password will need two columns to store in database. one is to store encrypted password and second column is to store salt used to encrypt the password.
index.php – This file plays role of accepting requests and giving response. This file accepts all GET and POST requests. on each request it will talk to database and will give appropriate response in JSON format.
Types of API JSON Responses
The following are the different types of JSON responses generated by API.
Registration Success Response – Success Code = 1 (User Successfully Stored)
Registration Error Response – Error Code = 1 (Error in storing)
Registration Error Response – Error Code = 2 (User Already Existed)
Login Success Response – Success Code = 1 (User Logged in)
Login Error Response – Error Code = 1 (Login Error – Incorrect username/password)
Here it completes the API part and start the Android Project.
3. Starting Android Project
Until now we wrote server side programming to build simple api. Next thing is build android app to interact with the API. In this project i am coding simple app which will have three screens Login Screen, Registration Screen and a welcome Dashboard Screen. So let’s get started by creating new project in you Eclipse IDE.
1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as DashboardActivity.
2. Next step is to create a new package to store all our library files. Right Click on ⇒ src ⇒ New ⇒ Package and name it as library.
JSON Parser Class
3. Next we need parser class to parse api response JSON. So create a new class in your library package name it as JSONParser.java and fill it with following code.
SQLite Database Handler Class
4. In the application to store user information i am using SQLite Database. So create new class in you library package folder and name it as DatabaseHandler.java and fill the class with following code. This class file has functions to handle database operations like storing user and getting user.
User Functions Class
5. Create a new class file under library package and name it as UserFunctions.java. This class will have functions to handle all user events like
In this class all the functions will interact with JSONParser, DatabaseHandler classes. I am testing API in localhost using xampp software. Normally localhost will run on port http://127.0.0.1 or http://localhost/. In AVD to connect to localhost you need to use url http://10.0.2.2/ instead of http://localhost/. If you want deploy your api on website the use the url http://yoursite.com/api/
Designing the Screens
6. Until now we have developed the library classes needed in this application. Next thing is build screens. We need three screens Login Screen, Registration Screen and Dashboard Screen.
Create 3 xml files under res ⇒ layout folder and name them as login.xml, register.xml and dashboard.xml
login.xml – login screen design layout
register.xml – registration screen design layout
dashboard.xml – dashboard screen design layout
Switching between Activites
7. Now the designing part of the app is done next thing is to create activities for each layout and write functionality to achieve login and registration process.
Create new activities LoginActivity.java and RegisterActivity.java and fill them with respective code below.
LoginActivity.java – Activity to handle login event
RegisterActivity.java – Activity to handle registration event
DashboardActivity.java – Activity to handle dashboard event
Finally Updating AndroidManifest.xml
Don’t forget to update you AndroidManifest.xml file. Change following modifications
⇒ Add Internet Persmissions
⇒ Add Entries of each Activity
8. Make sure that you have the files placed as in the following image
Run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.