How to Parse JSON From Node.js Express Domain Age Checker REST API in Android Application Using Volley Library in Java Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be parsing json in android application from domain age checker rest api using volley library in java. All the full source code of the tutorial will be given below.

 

 

 

Get Started

 

 

 

In order to get started you need to install the volley library using the gradle line as shown below. Include this line inside the build.gradle file as shown below

 

 

implementation 'com.android.volley:volley:1.2.0'

 

 

Now make the layout file using the activity_main.xml file as shown below

 

 

activity_main.xml

 

 

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:background="#000"
        android:capitalize="characters"
        android:fontFamily="sans-serif-black"
        android:text="Enter the Website or Domain to Check Age"
        android:textColor="#FFC107"
        android:padding="7dp"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Enter the Website or Domain to Check Age" />

    <EditText
        android:id="@+id/domainname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="496dp"
        android:ems="10"
        android:hint="Enter Domain"
        android:inputType="text"
        android:text="codingshiksha.com"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="396dp"
        android:onClick="getWebsiteAge"
        android:text="Get Domain Age"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.433"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="312dp"
        android:background="#000"
        android:capitalize="characters"
        android:fontFamily="sans-serif-black"
        android:padding="7dp"
        android:text="Domain Name"
        android:textColor="#FFC107"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.453"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="224dp"
        android:background="#000"
        android:capitalize="characters"
        android:padding="7dp"
        android:text="Domain Age"
        android:textColor="#FFC107"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.415"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="144dp"
        android:onClick="shareApp"
        android:text="Share App"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.419"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:onClick="blog"
        android:text="Official Blog Website"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="80dp"
        android:onClick="youtube"
        android:text="Official Youtube Channel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.562"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

See also  CreditCardView Rich UI for Creating Credit Card in Android

 

 

Now for the java code you need to copy paste the code inside the MainActivity.java file as shown below

 

 

MainActivity.java

 

 

package com.codingshiksha.domainagechecker;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private TextView domainName;
    private TextView domainAge;
    private RequestQueue mqueue;
    private RequestQueue mqueue1;
    private EditText domainUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        domainName = (TextView) findViewById(R.id.textView2);
        domainAge = (TextView) findViewById(R.id.textView3);
        domainUrl = (EditText) findViewById(R.id.domainname);

        getDomainAge();
    }

    public void getDomainAge(){
        mqueue = Volley.newRequestQueue(this);
        String url = "https://freemediatools.com/domainagechecker/url/codingshiksha.com";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            String domain = response.getString("domain");
                            String domainage = response.getString("domainage");
                            domainName.setText(domain);
                            domainAge.setText(domainage);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i("error",error.toString());

                    }
                });

// Access the RequestQueue through your singleton class.


        mqueue.add(jsonObjectRequest);

    }

    public void getWebsiteAge(View view){
        mqueue1 = Volley.newRequestQueue(this);
        String domainurl = domainUrl.getText().toString();
        domainurl = domainurl.replaceFirst("^(https://www\\.|http://www\\.|http://|https://|www\\.)","");
        String url = "https://freemediatools.com/domainagechecker/url/"+ domainurl;

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            String domain = response.getString("domain");
                            String domainage = response.getString("domainage");
                            domainName.setText(domain);
                            domainAge.setText(domainage);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i("error",error.toString());

                    }
                });

// Access the RequestQueue through your singleton class.


        mqueue1.add(jsonObjectRequest);

    }

    public void shareApp(View view){
        Intent myIntent = new Intent(Intent.ACTION_SEND);
        myIntent.setType("text/plain");
        String sharebody = "Domain or Website Age Checker Application Link : https://play.google.com/store/apps/details?id=com.codingshiksha.domainagechecker";
        String sub = "Domain Age Checker Android App";
        myIntent.putExtra(Intent.EXTRA_SUBJECT,sub);
        myIntent.putExtra(Intent.EXTRA_TEXT,sharebody);
        startActivity(Intent.createChooser(myIntent, "Share Using"));
    }

    public void blog(View view){

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.codingshiksha.com"));
        startActivity(browserIntent);

    }

    public void youtube(View view){
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/channel/UCR6d0EiC3G4WA8-Rqji6a8g"));
        startActivity(browserIntent);
    }
}

 

See also  Build a Digital Alarm Clock Android App in Android Studio Using Java Full Project For Beginners

 

Now copy paste the following line inside the AndroidManifest.xml file as shown below to include the internet permission

 

 

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

 

 

 

Now for the backend you need to install the below node.js library using the pip command as shown below

 

 

npm i whois-info

 

Leave a Reply