android register, android register using PHP and MySQL, android register with online authentication, android networking library

Easiest way to create a register application using android networking library

p>There have been several ways that are currently existing to help submit data from an android app to an online server so that it can be stored or processed to return a result

As an android developer, you should keep updated on the best methods that can help you securely transmit data from an android app to an online server without it being accessed by third-party libraries or users

Cases of hacking have been reported across the globe where users data has been stolen and their contact information used to enroll them in programs that they are not interested in or sending spam messages for promotions that do not exist

It is, therefore, necessary as a developer to use the best ways that are secure and fast. Our previous articles have focused on some of the methods you can use to send data securely from an android application to an online server using REST APIS

To ensure you are up to date with the article, you can revisit

For this discussion, we shall explain and show in detail how to create a register application using an android networking library or an application that submits data to an online server for storage using the android networking library.

For data processing in the server, we shall make use of a REST API which will be added in the android application as a URL, and also the parameters to hold data using the POST method

To begin,

  • create a project and name it as Register Project or any other name that you would wish, you can follow this article to create your application
  • the language of development we prefer using kotlin since it is the modern that is being adopted by google to develop android applications
  • in the activity_main.xml which will have the layout of the application, we shall have the first name, last name, email, username, and password as the fields that we will request users to provide in the app during registration

The code to add the above fields in the XML file will be as follows

<TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="First Name"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/firstname"

            android:inputType="text"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Last Name"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/lastname"

            android:inputType="text"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Email"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/email"

            android:inputType="textEmailAddress"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Username"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/username"

            android:inputType="text"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Password"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/password"

            android:inputType="textPassword"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

  • the full code for activity_main.xml is as follows

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <LinearLayout

        android:layout_marginTop="100dp"

        android:layout_marginStart="50dp"

        android:layout_marginEnd="50dp"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical">

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Fill Details to Register"

            android:layout_marginBottom="30dp"

            android:textSize="30dp"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="First Name"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/firstname"

            android:inputType="text"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Last Name"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/lastname"

            android:inputType="text"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Email"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/email"

            android:inputType="textEmailAddress"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Username"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/username"

            android:inputType="text"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Password"

            app:layout_constraintBottom_toBottomOf="parent"

            app:layout_constraintLeft_toLeftOf="parent"

            app:layout_constraintRight_toRightOf="parent"

            app:layout_constraintTop_toTopOf="parent" />

 

        <EditText

            android:id="@+id/password"

            android:inputType="textPassword"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>

        <Button

            android:text="REGISTER"

            android:id="@+id/signupbutton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"/>

 

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

  • next, in the manifest file add the internet permission that will allow the app be able to connect to internet connection as follows

<uses-permission android:name="android.permission.INTERNET" />

The full code for android manifest file is as follows

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.registerproject">

    <uses-permission android:name="android.permission.INTERNET" />

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/Theme.RegisterProject">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

  • to ensure that you use the networking library for data processing and submitting to the server, import this library in the build.gradle for the app

implementation 'com.amitshekhar.android:android-networking:1.0.2'

The code for build.gradle file for app in the dependencies section is as follows

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    implementation 'androidx.core:core-ktx:1.7.0'

    implementation 'androidx.appcompat:appcompat:1.4.0'

    implementation 'com.google.android.material:material:1.4.0'

    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'

    testImplementation 'junit:junit:4.+'

    androidTestImplementation 'androidx.test.ext:junit:1.1.3'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'com.amitshekhar.android:android-networking:1.0.2'

}

  • click sync now at the top of the screen and wait for gradle build to execute and finish, once its done, the networking library will be ready for use in your project
  • in the MainActivity.kt, first collect all the input fields from xml file as follows

var firstname=findViewById(R.id.firstname) as EditText

 var lastname=findViewById(R.id.lastname) as EditText

 var email=findViewById(R.id.email) as EditText

 var username=findViewById(R.id.username) as EditText

 var password=findViewById(R.id.password) as EditText

 var signupbutton=findViewById(R.id.signupbutton) as Button

  • when you click the signup button, ensure that all fields are not empty and if not, call the signup method as follows

signupbutton.setOnClickListener {

            var next = true

            if(firstname.toString().isEmpty()){

                next = false

            }

            if(lastname.toString().isEmpty()){

                next = false

            }

            if(email.toString().isEmpty()){

                next = false

            }

            if(username.toString().isEmpty()){

                next = false

            }

            if(password.toString().isEmpty()){

                next = false

            }

            if(next){

                signup(firstname.text.toString(),lastname.text.toString(),email.text.toString(),username.text.toString(),password.text.toString())

            }else{

            }

 

        }

  • add this method that uses the networking library functions to send data to the server using REST API as shown below

fun signup(firstname:String,lastname:String,email:String,username:String ,password:String){

        AndroidNetworking.post("https://www.solutionspacenet.com/apis/register")

            .addBodyParameter("firstname",firstname)

            .addBodyParameter("lastname",lastname)

            .addBodyParameter("email",email)

            .addBodyParameter("username",username)

            .addBodyParameter("password",password)

            .build()

            .getAsJSONObject(object :JSONObjectRequestListener{

                override fun onResponse(response: JSONObject?) {

                    if (response!!.getInt(STATUS) == 200) {

                        Toast.makeText(applicationContext,"Register successful",Toast.LENGTH_LONG).show()

                    } else {

                        Toast.makeText(applicationContext,"error",Toast.LENGTH_LONG).show()

                    }

                }

 

                override fun onError(anError: ANError?) {

                }

 

            })

    }

The full code for MainActivity.kt is as follows

package com.example.registerproject

import android.content.Context

import android.content.Intent

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import android.provider.ContactsContract.ProviderStatus.STATUS

import android.widget.Button

import android.widget.EditText

import android.widget.Toast

import com.google.android.material.snackbar.Snackbar

import org.json.JSONObject

import com.androidnetworking.AndroidNetworking

import com.androidnetworking.error.ANError

import com.androidnetworking.interfaces.JSONObjectRequestListener

import com.google.gson.Gson

 

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

 

        var firstname=findViewById(R.id.firstname) as EditText

        var lastname=findViewById(R.id.lastname) as EditText

        var email=findViewById(R.id.email) as EditText

        var username=findViewById(R.id.username) as EditText

        var password=findViewById(R.id.password) as EditText

        var signupbutton=findViewById(R.id.signupbutton) as Button

 

        signupbutton.setOnClickListener {

            var next = true

            if(firstname.toString().isEmpty()){

                next = false

            }

            if(lastname.toString().isEmpty()){

                next = false

            }

            if(email.toString().isEmpty()){

                next = false

            }

            if(username.toString().isEmpty()){

                next = false

            }

            if(password.toString().isEmpty()){

                next = false

            }

            if(next){

                signup(firstname.text.toString(),lastname.text.toString(),email.text.toString(),username.text.toString(),password.text.toString())

            }else{

            }

 

        }

 

 

    }

    fun signup(firstname:String,lastname:String,email:String,username:String ,password:String){

        AndroidNetworking.post("https://www.solutionspacenet.com/apis/register")

            .addBodyParameter("firstname",firstname)

            .addBodyParameter("lastname",lastname)

            .addBodyParameter("email",email)

            .addBodyParameter("username",username)

            .addBodyParameter("password",password)

            .build()

            .getAsJSONObject(object :JSONObjectRequestListener{

                override fun onResponse(response: JSONObject?) {

                    if (response!!.getInt(STATUS) == 200) {

                        Toast.makeText(applicationContext,"Register successful",Toast.LENGTH_LONG).show()

 

                    } else {

                        Toast.makeText(applicationContext,"error",Toast.LENGTH_LONG).show()

                    }

                }

 

                override fun onError(anError: ANError?) {

                }

 

            })

    }

}

<?php

header("Content-Type:application/json");

$con=mysqli_connect("localhost","username","password","database") or die("error");

$firstname = $_POST["firstname"];

$lastname = $_POST["lastname"];

$email = $_POST["email"];

$username = $_POST["username"];

$password = $_POST["password"];

if(mysqli_query($con,"INSERT INTO customers(FirstName,LastName,Email,Username,Password) VALUES('$firstname','$lastname','$email','$username','$password'")){

$json = array("status" => 200,'message' => "Success");

}else{

$json = array("status" => 300,'message' => "Error");

}

echo json_encode($json);

mysqli_close($con)

 

?>

  • Finally, Run your application and install the apk file in your mobile device