Volley Library - Registration, Log-in, and Log-out
In this tutorial, we will create the basic user registration and log-in module using Volley library and JSON. Volley is a HTTP Library which provides the facilities for network connectivity for our app.
The advantages of using the Volley library are as follows:
For the server-side data handling, we are using the PHP with XAMPP server and MySQL for data manipulation.
Before creating the Android application module, let's first create the server-side data handling API code for registration and log-in in PHP and MySQL.
1. Create a database with the name registerlogin and table users which contains the following fields.
2. Write the following connection establishment code with PHP and database inside C:\xampp\htdocs\androidphpmysql directory.
In the androidphpmysql (your project location) directory, create a connection.php file and write the following code.
3. Create a registrationapi.php file in androidphpmysql directory and write the following code. This file handles the request coming from an Android application and generates a response in the form of JSON array to the android application.
To check your PHP API whether it is working fine or not, you can use the REST client such as Postman tool.
To check the registration code of API, you can pass the URL of registration with
Similarly, you can check the log-in action by passing log-in URL with valid
Now, in the Android application, we will create the three activity class for user registration, user log-in and display the user detail in main activity (as a profile).
Create an activity_main.xml in layout and add the below code. This activity is used for displaying the detail of the user as a profile.
Now, create an activity_login.xml file in layout directory with following code. This activity is used for user log-in UI.
Create an activity_register.xml file in layout directory with following code. This activity is used for user registration UI.
Add the volley library dependency in build.gradle file.
Create a data model class named as User.java with the following code.
We need to define our URL that call the API of server-side.
Create an URLs.java class and define the URL.
Create a class named as SharedPreferences.java. In this class, we use the SharedPreferences class to store the user detail. The SharedPreferences class contains four methods with the following functionalities:
Now, in the MainActivity.java class, we will display the user information if the user is log-in otherwise, it redirects to LoginActivity.java class. The onClick() method is used to log-out the user when clicking on the button.
In the LoginActivity.java class, we check if the user is already log-in or not, if true then redirect to MainActivity.java class otherwise, allow a user to log-in.
StringRequest class of Volley library is used for network module. The object of StringReuest class takes the parameters of the request method type, URL, and the response.
The RegisterActivity.java class is used to register the user. This class initially checks the user log-in if it is true, then redirect to MainActivity.java class otherwise, allow user for registration.
Similar to LoginActivity.java class, we use the StringRequest class of Volley library for network connection and pass the parameters of the type request method, URL, and the response. The Response.Listener<String>() method handles the response generated by the server.
Add the following permission in AndroidManifest.xml file