Professional Documents
Culture Documents
EXP6
EXP6
In any Android Application, there is only so much a user can do without an internet
connection. All modern Android Applications interact with resources available online or a backend-
specific to the app. In this article, we will look at one of the ways through which we can retrieve and
post resources online through HTTP Requests. We will use the Volley Library for handling HTTP
requests.
implementation 'com.android.volley:volley:1.2.1'
2. Glide Image Processing Library for Caching and Loading Images from the Image URL Retrieved
from the HTTP Request.
implementation 'com.github.bumptech.glide:glide:4.13.2'
Navigate to app > manifests > AndroidManifest.xml and add the piece of code given below to the
file.
<uses-permission android:name="android.permission.INTERNET"/>
XML
<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">
<!-- This ImageView is used to show the dog images to the user -->
<ImageView
android:id="@+id/dogImageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/nextDogButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginHorizontal="6dp"
android:layout_marginBottom="10dp"
tools:srcCompat="@tools:sample/avatars" />
<!-- This Button is used for making a new HTTP request for fetching new dog image -->
<Button
android:id="@+id/nextDogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Dog"
android:padding="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="30dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin
Java
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
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
// be loaded
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mDogImageView = findViewById(R.id.dogImageView)
nextDogButton = findViewById(R.id.nextDogButton)
nextDogButton.setOnClickListener { loadDogImage() }
loadDogImage()
Request.Method.GET,
url,
null,
{ response ->
Glide.with(this).load(dogImageUrl).into(mDogImageView)
},
volleyQueue.add(jsonObjectRequest)