Kotlin Android Web Service - Volley Library Registration, Log-in, and Log-out Tutorial
In this tutorial, we will create the basic user registration and log-in module using Volley library and JSON. Volley is an HTTP Library which provides the facilities for network connectivity for our app.
The web API of registration and login is created using PHP with MySQL. Before implementing the client side Android application code, first look at the server side API code at https://www.javatpoint.com/android-volley-library-registration-login-logout.
In 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 following code. This activity is used to display the detail of the user profile.
Now, create an activity_login.xml file in the layout directory with following code. This activity is used for user log-in UI.
Create an activity_register.xml file in the 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.kt with the following code.
We need to define our URL that calls the API of server-side.
Create an URLs.kt class and define the URL.
Create a class named as SharedPreferences.kt. 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.kt class, we will display the user information if the user is logged-in otherwise, it redirects to LoginActivity.kt class. The onClick() method is used to log-out the user when clicking on the button.
In the LoginActivity.kt class, we check that the user is already logged-in or not, if true then redirect to MainActivity.kt class otherwise, allow a user to log-in.
StringRequest class of Volley library is used for network module. The object of StringRequest class takes the parameters of the type request method, URL, and the response.
The RegisterActivity.kt class is used to register the user. This class initially checks the user log-in, if true then redirect to MainActivity.kt class otherwise, allow user for registration.
Similar to LoginActivity.kt 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 handles the response generated by the server.
Add the following permission in AndroidManifest.xml file
Note: The server side code of registration and login are implemented in PHP with MySQL at https://www.javatpoint.com/android-volley-library-registration-login-logout.