android-logo

Volley Android

Table of Content

  • Advantages of Volley Library
  • Disadvantages of Volley Library

Overview

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.



Advantages of Volley Library

  1. It allows caching.
  2. The tool provides tracing and debugging capabilities.
  3. Multiple connections can be made concurrently.
  4. The library supports OkHttp.
  5. Using it, network connections can be automatically scheduled.

Disadvantages of Volley Library

  1. Large downloads and streaming are not suitable.

Sample JSON link

https://www.androidaura.com/json-files/avengers-movie.json


Postman tool to check our API calls

volley-library

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


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


Step 2 - Adding internet permission to AndroidManifest.xml


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


Step 3 - Add code to MainActivity


package com.androidaura.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.androidaura.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);
    }
}


Step 4 - Design a Layout in XML


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

Output
volley-library-1

previous-button
next-button

facebook-logo
twitter-logo
instagram-logo

2020 © All rights reserved.