The Volley Library was announced at Google I/O 13 by Ficus Kirkpatrick. With Volley, you can make Android app networking easier and faster. Using this library, you can send data across a network. A repository for the project has been established in the AOSP (Android Open Source Project).
In this tutorial we will see how to use Volley libaray to make Restful API calls.
dependencies{
implementation 'com.android.volley:volley:1.1.0'
}
<uses-permission android:name="android.permission.INTERNET />
package com.techstackguru.volley;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
TextView tv1,tv2,tv3,tv4,tv5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = findViewById(R.id.moviename);
tv2 = findViewById(R.id.tagline);
tv3 = findViewById(R.id.storyline);
tv4 = findViewById(R.id.releaseyear);
tv5 = findViewById(R.id.imdbrating);
String myUrl = "https://www.techstackguru.com/json-files/avengers-movie.json";
StringRequest myRequest = new StringRequest(Request.Method.GET, myUrl,
response -> {
try{
JSONObject jsonObject = new JSONObject(response);
tv1.setText(jsonObject.getString("movieName"));
tv2.setText(jsonObject.getString("tagLine"));
tv3.setText(jsonObject.getString("storyLine"));
tv4.setText(jsonObject.getString("releaseYear"));
tv5.setText(jsonObject.getString("imdbRating"));
} catch (JSONException e) {
e.printStackTrace();
}
},
volleyError -> Toast.makeText(MainActivity.this, volleyError.getMessage(), Toast.LENGTH_SHORT).show()
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(myRequest);
}
}
<?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"
android:orientation="vertical"
android:layout_margin="20dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Movie Name"
android:textColor="#03A9F4"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/moviename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20sp"
android:textColor="#424242"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tag Line"
android:textColor="#03A9F4"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tagline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20sp"
android:textColor="#424242"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Story Line"
android:textColor="#03A9F4"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/storyline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20sp"
android:textColor="#424242"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Release Year"
android:textColor="#03A9F4"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/releaseyear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20sp"
android:textColor="#424242"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="IMDB Rating"
android:textColor="#03A9F4"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/imdbrating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20sp"
android:textColor="#424242"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>