Javatpoint Logo
Javatpoint Logo

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.

activity_main.xml

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

activity_login.xml

Create an activity_register.xml file in the 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.kt with the following code.

User.kt

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

Create an URLs.kt class and define the URL.

URLs.kt

VolleySingleton.kt

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:

  • userLogin(): This function 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.
  • user: User get(): This method gets the user information if log-in.
  • logout(): This function clears the SharedPreferences data and makes user log-out.

SharedPrefManager.kt

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.

MainActivity.kt

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.

LoginActivity.kt

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.

RegisterActivity.kt

AndroidManifest.xml

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.

Output:

Kotlin Android Web Service Kotlin Android Web Service
Kotlin Android Web Service Kotlin Android Web Service
Kotlin Android Web Service Kotlin Android Web Service
Kotlin Android Web Service Kotlin Android Web Service
Kotlin Android Web Service Kotlin Android Web Service
Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA