How to Get Data to API using Volley in Android?

 

  Using  Get Method  Retrieve Data from Server :



Step 1 – Design a Layout

In this step, we will design the UI for the layout of our application. Our layout has six TextViews. Three are labels and the other three are empty text views that will display the information from the API. Open activity_main.xml and add the following lines of code;

<?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"
android:background="@color/white">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Cases in the World"
android:textSize="22dp"
android:textStyle="bold"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.225" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Recovered in the World"
android:textSize="22dp"
android:textStyle="bold"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Deaths in the World"
android:textSize="22dp"
android:textStyle="bold"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.559" />

<TextView
android:id="@+id/newCasesWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/teal_200"
android:textSize="22dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296" />

<TextView
android:id="@+id/newDeathsWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/teal_200"
android:textSize="22dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.654" />

<TextView
android:id="@+id/newRecoveredWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/teal_200"
android:textSize="22dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.473" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 2 – Adding Volley to our Application.

Add the following dependency to the app module-level build.gradle file:

dependencies{
 implementation 'com.android.volley:volley:1.1.0'
}

In your AndroidManifest.xml add internet permission:

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

Step 3– Making the API Requests

In this step, we’ll make the API request. We’ll be using the NovelCOVID API, that contains live Corona Virus statistics.


In the getData method, create a StringRequest and assign the NovelCOVID API URL (https://corona.lmao.ninja/v2/all) to a String. We are using the StringRequest because we want to return information in String form.

Next, we will create a ResponseListener, that will contain an onResponse method. In the onResponse method, we’ll create a JSONObject class. This class will pass the API data and then set the data from API to the respective views.

Note: This should be done within a try block. The parameters inside the getString()should be the same as the name given in the JSON format.

In the onErrorResponse method, we’ll show a Toast message in case of an error.

  • Activity Main.java code :

public class MainActivity extends AppCompatActivity {

private TextView totalCasesWorld, totalDeathsWorld, totalRecoveredWorld;

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

//Initialize the objects
totalCasesWorld = findViewById(R.id.newCasesWorld);
totalDeathsWorld = findViewById(R.id.newDeathsWorld);
totalRecoveredWorld = findViewById(R.id.newRecoveredWorld);

getData();

}
private void getData(){
String myUrl = "https://corona.lmao.ninja/v2/all";
StringRequest myRequest = new StringRequest(Request.Method.GET, myUrl,
response -> {
try{
//Create a JSON object containing information from the API.
JSONObject myJsonObject = new JSONObject(response);
totalCasesWorld.setText(myJsonObject.getString("cases"));
totalRecoveredWorld.setText(myJsonObject.getString("recovered"));
totalDeathsWorld.setText(myJsonObject.getString("deaths"));
} catch (JSONException e) {
e.printStackTrace();
}
},
volleyError -> Toast.makeText(MainActivity.this,
volleyError.getMessage(), Toast.LENGTH_SHORT).show()
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(myRequest);
}


}






Rajeshbhatt12

My name is Rajesh Bhatt. I am working as a senior android developer . I have created this blog for kotlin ,java and Android Development interview questions etc..

Previous Post Next Post