Do somthings

if you are a beginner in android then you confused about what is API and how to use it in your android apps. don’t worry we are here to help you.

First, you understand what is API. API stands for an Application programming interface. it means communicating and creating connections with two applications. One common API usage the example we come across on a daily basis is weather data, Another prominent example of API usage is the “log-in using many websites.

I thoughts you understand with API now you learn how to build apps and how to connect with API.

in this project, we are JSON Parsing in Android using Volley Library. JSON is also known as (JavaScript Object Notation) is a format to exchange the data from the server. The data stored in JSON format is lightweight and easy to handle. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringe. In this article, we will specifically take a look at the implementation of JsonObject using Volley in Android.

Note: JSON Object: Json Object can be easily identified with “{” braces opening and “}”
braces closing. We can fetch data from JSON objects with their key value. From that key, we can access the value of that key.

What we are going to build in this article?

We will be building a simple application in which we will be displaying a simple CardView in which we will display Covid cases worldwide. we have to use https://corona.lmao.ninja API to collect the data. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio click File->New->New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add the below dependency in your build.Gradle file.

// below line is used for volley library

implementation ‘com.android.volley:volley:1.2.1’

// below line is used for image loading library

implementation ‘com.github.bumptech.glide:glide:4.12.0’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.12.0’

After adding this dependency please sync your project and now move towards the AndroidManifest.xml part.

Step 3: Adding permissions to the internet in the
AndroidManifest.xml file

Navigate to the app > AndroidManifest.xml and add the below code to it.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapi">

<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.MyFirstAPI">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add
the below code to that file. Below is the code for the
activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
android:layout_margin="5dp"
android:padding="5dp"
app:cardCornerRadius="5dp">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cases"
android:gravity="left"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>

<TextView
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null"
android:gravity="right"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Today Cases"
android:gravity="left"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>

<TextView
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null"
android:gravity="right"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Deaths"
android:gravity="left"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>

<TextView
android:id="@+id/three"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null"
android:gravity="right"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Today Deaths"
android:gravity="left"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>

<TextView
android:id="@+id/four"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null"
android:gravity="right"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recovered"
android:gravity="left"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>

<TextView
android:id="@+id/five"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null"
android:gravity="right"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Today Recovered"
android:gravity="left"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>

<TextView
android:id="@+id/six"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null"
android:gravity="right"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"/>


</LinearLayout>


</LinearLayout>


</androidx.cardview.widget.CardView>



</LinearLayout>

Output:

Step 5: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

package com.example.myfirstapi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

import java.net.URL;

public class MainActivity extends AppCompatActivity {


public static final String URL = "https://disease.sh/v3/covid-19/all";

TextView One,Two,Three,Four,Five,Six;

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


//Hook's
One = (TextView) findViewById(R.id.one);
Two = (TextView) findViewById(R.id.two);
Three = (TextView) findViewById(R.id.three);
Four = (TextView) findViewById(R.id.four);
Five = (TextView) findViewById(R.id.five);
Six = (TextView) findViewById(R.id.six);


//Calling API..


getData();

}

private void getData() {

StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

try {
JSONObject object =new JSONObject(response);

String cases = object.getString("cases");
String tCaes = object.getString("todayCases");
String Deaths = object.getString("deaths");
String tDeaths = object.getString("todayDeaths");
String Recover = object.getString("recovered");
String tRecover = object.getString("todayRecovered");

//now the set the value in TextViews.....
One.setText(cases);
Two.setText(tCaes);
Three.setText(Deaths);
Four.setText(tDeaths);
Five.setText(Recover);
Six.setText(tRecover);


} catch (JSONException e) {
e.printStackTrace();
}


}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

//if code give error then this code is run....
Toast.makeText(MainActivity.this, "We can't process this time!!!", Toast.LENGTH_SHORT).show();
}
});

Volley.newRequestQueue(this).add(stringRequest);
}


}

Now run your app and see the output of the app.

Categories: Android

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *