Javatpoint Logo
Javatpoint Logo

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:

  • Easier and faster request management.
  • Provide efficient network management.

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.


android Volley Library - Registration Log-in and Log-out

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.

connection.php

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.

registrationapi.php

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 parameters.


android Volley Library - Registration Log-in and Log-out

Similarly, you can check the log-in action by passing log-in URL with valid parameters.


android Volley Library - Registration Log-in and Log-out

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.

activity_main.xml

Now, create an activity_login.xml file in layout directory with following code. This activity is used for user log-in UI.

activity_login.xml

Create an activity_register.xml file in layout directory with following code. This activity is used for user registration UI.

activity_register.xml

Add the volley library dependency in build.gradle file.

build.gradle

Create a data model class named as User.java with the following code.

User.java

We need to define our URL that call the API of server-side.

Create an URLs.java class and define the URL.

URLs.java

VolleySingleton.java

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:

  • userLogin(): This method is used to store the user information in SharedPreferences after log-in.
  • isLoggedIn(): This method checks whether the user is already log-in or not.
  • getUser(): This method get the user information if log-in.
  • logout(): This method clear the SharedPreferences data and makes user log-out.

SharedPrefManager.java

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.

MainActivity.java

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.

LoginActivity.java

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.

RegisterActivity.java

Add the following permission in AndroidManifest.xml file

AndroidManifest.xml

Output:

android Volley Library - Registration Log-in and Log-out android Volley Library - Registration Log-in and Log-out
android Volley Library - Registration Log-in and Log-out android Volley Library - Registration Log-in and Log-out
android Volley Library - Registration Log-in and Log-out
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials