In the previous article, we learned about Volley library with a simple presentation. In this article, we are going to create an Android application with the Volley. Volley is a networking library managed by the RequestQueue and mainly used for smaller Networking purposes in Android. Volley is available through the AOSP repository.
Volley library is an HTTP library for Android apps that makes networking simple and fast. It was created by Google and debuted at Google I/O 2013. It was created in response to the lack of a networking class in the Android SDK that can function without interfering with the user experience. It manages network request processing and caching, saving developers time from writing the same network call/cache code over and over. Volley library is not suitable for large download or streaming operations because it holds all resources.
Contents
Output
Advantages of Volley
- Queuing and prioritization of requests.
- Automatic scheduling of network requests.
- Memory and request cache management that is effective.
- The library’s extensibility and adaptability to our needs.
- Cancellation of requests or block of requests.
- Ease of customization.
Classes in Volley Library
Two main classes are in Volley:
- Request Queue: It is the interest that is used to send network requests. If required, a request queue can be placed on demand, but usually is created early and during startup, kept around, and used as a singleton.
- Request: It contains all information necessary to make a Web API call. It is the base for creating network requests(GET, POST).
What is Glide?
- Glide is an Image Loader Library for Android developed by bumptech and is a Google-recommended library. It’s been used in a number of Google open-source projects. It supports animated GIFs and handles image loading/caching.
- Glide is an open-source media management and image loading framework for Android that wraps media decoding, memory and disc caching, and resource pooling into a simple and easy-to-use interface.
Prerequisite
You should know the basic knowledge of android apps, folder structures, debugging, and running applications.
Requirements
- To get the data, you’ll need to use a third-party API.
- Volley library is required to call that API.
- A glide library is required for Image loading.
In this example, I’m using the https://github.com/D3vd/Meme_Api site to get the API(Free).
One needs to import Volley and add permission to the Android Project before starting with Volley.
Step-by-step explanation:
Step 1: Create a new project.
Step 2: Open build.gradle(Module: app) and add the following dependency:
- In this project, we will use the Glide library for image loading.
- In this project, we will use Volley to call the API.
//volley library implementation 'com.android.volley:volley:1.2.0' //Glide library implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
Step 3: Add internet permission to AndroidManifest.xml file
- Because we work with network operations and other such things, we require Internet connection permission.
//internet permission <uses-permission android:name="android.permission.INTERNET"/>
- Below is the code of the AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sagar.volleyexample"> <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.VolleyExample"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Step 4: To use the volley library in the android application, we must first define a layout for it. (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"> <ImageView android:id="@+id/imageView" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/ButtonNext" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" tools:srcCompat="@tools:sample/avatars" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="@id/imageView" app:layout_constraintBottom_toBottomOf="@id/imageView" app:layout_constraintLeft_toLeftOf="@id/imageView" app:layout_constraintRight_toRightOf="@id/imageView"/> <Button android:id="@+id/ButtonNext" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_marginBottom="16dp" android:onClick="next" android:padding="20dp" android:text="@string/next" android:textColor="@color/black" android:textSize="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Make a GET request/ Make an HTTP request with the volley Library.
- So far, we’ve successfully created a layout for an application, Now we’ll look at how to make an HTTP request with the volley library.
Using the volley library, we can send GET, PUT, POST, PATCH, and DELETE requests. In this android application, we will now make an HTTP request using the volley library. - We’ll use volley to make a GET request that returns all data.
Insert the following code into your MainActivity.kt file.
package com.sagar.volleyexample import android.graphics.drawable.Drawable import android.os.Bundle import android.view.View import android.widget.ImageView import android.widget.ProgressBar import androidx.appcompat.app.AppCompatActivity import com.android.volley.Request import com.android.volley.toolbox.JsonObjectRequest import com.android.volley.toolbox.Volley import com.bumptech.glide.Glide import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) loaddata() } private fun loaddata() { val progressBar=findViewById<ProgressBar>(R.id.progressBar) progressBar.visibility=View.VISIBLE val queue = Volley.newRequestQueue(this) val url = "https://meme-api.herokuapp.com/gimme" val imageView = findViewById<ImageView>(R.id.imageView) val JsonObjectRequest = JsonObjectRequest( Request.Method.GET, url,null, { response -> val currentImageUrl=response.getString("url") Glide.with(this).load(currentImageUrl).listener(object:RequestListener<Drawable>{ override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean { progressBar.visibility=View.GONE return false } override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { progressBar.visibility=View.GONE return false } } ).into(imageView) }, { }) queue.add( JsonObjectRequest) } fun next(view: View) { loaddata() } }
Run the application now. You will get the output shown in the output section.
- We hope that this guide will assist you in quickly creating a simple android Application using the volley library in android. We concentrated on making a simple application for this tutorial. If you have any problems, please post them in the comments section, and we will gladly assist you.
Pingback: Glide library in Android with Example - Developers Dome