Android Login and Registration with PHP, MySQL and SQLite
Show
DOWNLOAD CODE VIDEO DEMO
PrerequisitesThis tutorial is combination of few of my previous articles. Make sure you went through the below articles if you are very beginner. 1. Android working with Volley Library – Used to make HTTP calls. 2. Android Login and Registration Screen Design – Explained the designing of Login and Registration screens. API (Application Programming Interface)To interact with MySQL database we need to build a REST API first. REST Api job is to get the request from client, interact with database and finally give the response back to client. So we’ll create a simplePHP, MySQL API first. Our API do’s below jobs. ⇒ Accepts requests in GET/POST methods
1. Downloading & Installing WAMPDownload & Install WAMP server from www.wampserver.com/en/. Once installed, launch the program from Start ⇒ All Programs ⇒ WampServer ⇒ StartWampServer. If you are on Mac, alternatively you can use MAMP for the same. You can test your server by opening the address http://localhost/ in your browser. Also you can check phpmyadmin by opening http://localhost/phpmyadmin Following is a screencast of Downloading and Installing WAMP Server.
2. Creating MySQL Database and TablesOpen phpmyadmin and execute below queries to create necessary database and table. Here we are creating only one table users to store users login information.
3. Creating PHP ProjectGoto the location where wamp installed and open www folder. The default installation location of wamp would be C:/wamp. Below is the final PHP project structure we are going to create in this article.
2. In android_login_api, create another directory named include. In this folder, we keep all the helper classes. 3. Now inside include, create a php file named Config.php and add below content. Replace theDB_USER and DB_PASSWORD values with your’s. Config.php
DB_Connect.php
3.1 Registration EndpointNow we have all the required classes ready. Let’s start creating the endpoint for user registration. This endpoint accepts name, email and password as POST parameters and store the user in MySQL database. 6. In android_login_api root directory, create register.php and below code. register.php
3.2 Login EndpointJust like registration, we need to create another endpoint for login. This endpoint accepts email and password as POST parameters. After receiving the email and password, it checks in database for matched user. If the user is matched, it echoes the success the json response. 7. Create a php file named login.php inside android_login_api with the below content. login.php
3.3 Types of JSON ResponsesThe following are the different types of JSON responses for registration and login endpoints. 3.3.1 Registration URL: http://localhost/android_login_api/register.php PARAMS: name, email, password Registration success response
3.3.2 Login URL: http://localhost/android_login_api/login.php PARAMS: email, password Login Success
Now we have completed the PHP part. Let’s start the android part. 4. Creating the Android ProjectThe app we’re gonna build will have three simple screens Login Screen, Registration Screen and a welcome Dashboard Screen. 1. In Android Studio, create a new project from File ⇒ New Project and fill all the required details. 2. Create three packages named app, activity and helper under src folder. 3. Open build.gradle and add volley library support by adding build.gradle
strings.xml
colors.xml
6. Under app package create a class named AppConfig.java and add below code. In this class we declare the login and registration urls. While testing you need to replace the ip address with your localhost pc ip. AppConfig.java
AppController.java
AndroidManifest.xml
My previous article Android User Session Management using Shared Preferences gives you a good overview of managing the session data using SharedPreferences. SessionManager.java
SQLiteHandler.java
4.1 Adding the Login ScreenNow we’ll implement the login module by creating the screen and adding the code to make login request to php, mysql server. 11. Create an xml file named activity_login.xml under res ⇒ layout. activity_login.xml
checkLogin() – Method verifies the login details on the server by making the volley http request. LoginActivity.java
4.1 Adding the Registration Screen13. Create an xml layout named activity_register.xml under res ⇒ layout. activity_register.xml
registerUser() – Will store the user by passing name, email and password to php,mysql server. db.addUser() – Will insert the user in SQLite database once he is successfully registered. RegisterActivity.java
4.3 Adding the Home ScreenUntil now we are done with both login and registration screen. Now we’ll add the final screen to show the logged in user information. This information will be fetched from SQLite database once user is logged in. 15. Create an xml file named activity_main.xml under res ⇒ layout and add below code. activity_main.xml
MainActivity.java
5. Testing the AppFor a beginner it will be always difficult to run this project for the first time. But don’t worry, the following steps will helps you testing this app. ⇒ Make sure that both devices (the device running the PHP project and the android device) are on the same wifi network. ⇒ Give correct username , password and database name of MySQL in Config.php ⇒ Replace the URL ip address of URL_LOGIN and URL_REGISTER in AppConfig.java with your machine ip address. You can get the ip address by running ipconfig in cmd
What’s Next?If you understand this article very clearly, its time to improvise your knowledge by following the below articles. |