Android Registration and Login System using PHP,MySQL and Retrofit Square API

In this, tutorial I am going to explain how to build a complete Android Registration and Login System using PHP, MYSQL and Retrofit Square API.This tutorial also covers the basics that how we can build a simple REST API for Android using PHP and MYSQL.

 API (Application Programming Interface)

To interact with the database, we need an API which will take the request from the Android device, perform the operation on the database accordingly and give the response back to the caller device which is specifically the Android device in our case.

⇒ Accepts requests in GET/POST methods.

⇒ Interact with the database by inserting/fetching data.

⇒ finally will give response back in JSON format.

 

 

Android Signup and Login using PHP,MySQL,Retrofit

I divide this tutorial into two half.

1- Create the RESTAPI for Login And Registration

2- Design and Develop the Android App.

 

Create the RESTAPI for Registration and Login System

1. Downloading & Installing XAMPP OR Purchase Hosting:

To work on local Server Download & Install XAMPP server from https://www.apachefriends.org/download.html . Once installed, launch the program XAMPP. If you are on Mac, alternatively you can use MAMP. You can test your server by opening the address http://localhost/ in your browser. Also, you can check PHPMyAdmin by opening http://localhost/phpmyadmin.

2. Creating MySQL Database and Tables:

Open the PHPMyAdmin and then create a database named “register_login” and a Table “User”. You can do this by Executing below query in SQL tab

3) Creating PHP Script using SLIM Framework to communicate with MYSQL and to generate Endpoint for android app:

3.1) What is Slim Framework.

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. You can download the Slim framework from this link https://github.com/slimphp/Slim  or you can use my source code.

3.2) Database connection Method:

3.3) Login Function which will Receive Http Post Request:

3.4) SignUP Function which will Receive Http Post Request:

3.5) Put the source code folder i.e “register-login “in xampp=>htdocs:
Now PHP scripts are hosted on a local server.
If you are using real hosting you can put the folder there and call it from URL accordingly.

4) Testing the RESTAPI using Chrome extension POSTMAN:
We can test our RESTAPI using a very handy chrome extension “POSTMAN.
You can download it from this link http://www.getpostman.com/
Once you download then open it from chrome by typing this URL chrome://apps/
then choose Postman

Android Signup and Login using PHP,MySQL,Retrofit with postman testing

4.1) Testing SignUp Method:

1.Choose request type POST and type the URL http://localhost/register-login/v1/signup.
2.Enter form data email, name and password and click send.
3.You will see a JSON response.
Status ==1 it means user successfully registered
Status==0 it means some user already registered with that email.

Android Signup and Login using PHP,MySQL,Retrofit with postman testing method

4.2) Testing Login Method:
1. Choose request type POST and type the URL http://localhost/register-login/v1/login
2. Enter form data email and password and click send
3. You will see a JSON response.
Status ==1 it means user successfully login
Status==0 it means login failed.

Android Signup and Login using PHP,MySQL,Retrofit with postman testing method login

Design and Develop the Android App

5. Create a new application in the android studio and Importing Required Libraries:
Open android Studio and create a new project.
Open build.gradle file adds Square Retrofit and OKHTTP and rebuild the project and Internet permission in android manifest.

Android Signup and Login using PHP,MySQL,Retrofit dependencies

Android Signup and Login using PHP,MySQL,Retrofit dependencies 2

Retrofit Introduction:

Retrofit is a type-safe REST client for Android built by Square. What makes Retrofit different is that it gives a unique feature through which we can define java interfaces with annotations, which can be further used to send parameters. These interfaces are the only access points in our code through which we will be making REST web API calls. The process of making a REST web API call through Retrofit in Android is simple.

6) Generating Model class (POJO) from JSON response:
Now we will make all POJO of JSON data coming from the server.
How to Make POJO class in Retrofit by Most Easiest Way:
1. Visit JsonSchema2pojo.org – This is an Official website to Convert JSON Data into POJO class. Just You have to Copy Paste all the Codes.
2. Open JsonSchema2pojo its looks like below picture.
3. Paste your JSON data in the box at left side. check source type JSON and Annotation style Gson.

Android Signup and Login using PHP,MySQL,Retrofit JSON POJO

Click on preview and you will see all pojo class are created like below.

Android Signup and Login using PHP,MySQL,Retrofit POJO 2

You can define class name for callback or by default it is Example.java in my case name of call back class is LoginModel. copy all class and paste into your project I will suggest you to make a Different Package for all POJO class like my project.

7) Creating REST Interface (necessary for Retrofit API) which is to used later for calling:

You change the urlaccoding to your system IP Address if you are working on localhost

If you are using hosting you can change it according to your domain name path.

8) Designing Login Screen and adding login functionality:

activity_login.xml here is the XML file.

It will look like this.

Android Signup and Login using PHP,MySQL,Retrofit Login2

Now come LoginActivity.javahere we will create an object of this RESTInterfacelike below code.

Now call Login method with Callback like below. One more thing make CallBack of Retrofit not of java you can see When you will type by pressing ctrl+space.

Full Source Code of LoginActivity

8) Designing Signup Screen and adding SignUpfunctionality:

 

activity_signup.xml code

It will look like this.

Android Signup and Login using PHP,MySQL,Retrofit Sign up

SignupActivity.Java code:

Here we call signup function from the RESTInterface. Full code is given below.

You can always ask for help. I am available for your queries.

That’s it Happy coding.

 

 

One Important Notes:

If you are working on localhost, by default XAMPP is not accessible in the network. You need to make these changes

All you have to do is to edit the httpd-xampp.conf

from Require local to Require all granted in the LocationMatch tag.

That’s it!

Conclusion:

So in this tutorial, we see how to make Android Registration and Login System using PHP, MySQL and Retrofit Square API.If you find this information useful, remember to share it on your social network. If you have any specific concerns/doubt about this article you can comment below and let us know.

 





download-code excel export data




Read previous post:
create-dynamic-pdf-using-php-fpdf-library
Create Dynamic PDF using PHP FPDF Library

In this tutorial, we are going to see how to Create Dynamic PDF using PHP FPDF Library. I am going to...

Close