Introduction
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. It is available on GitHub.
Code
In this article, We will be creating a small android project using Kotlin. I have chosen Kotlin as it provides faster development compared to java. You can choose java if you want to. The source code is available at GitHub
Step 01
Create an android studio project, and name it whatever you like.
Step 02
Add the following permission in the manifest file since we would be fetching data from the internet:
<uses-permission android:name="android.permission.INTERNET"/>
Step 03
Add the following dependency to your grade file. You may update the version number based on the version of your tool set.
implementation 'com.android.volley:volley:1.2.1'
Step 04
Create a queue that will manage our volley requests. The following line of code would do it for us:
val queue = Volley.newRequestQueue(this)
here, this refers to the current application context. You may include it anywhere in your code. For simplicity add it in onCreate
method of your activity.
Step 05
Create a request object. There are four types of requests available in volley:
- JsonObjectRequest
- JsonArrayRequest
- ImageRequest
- StringRequest
We would be working with StringRequest. It requires four things from us:
- Method: GET, POST, etc.
- Source link
- Listener for success
- Listener for failure
Here's the code
val request = StringRequest(
Request.Method.GET,
"https://jsonplaceholder.typicode.com/users",
Response.Listener<String> {
findViewById<TextView>(R.id.textview).text = it
},
Response.ErrorListener {Log.d("error",it.toString())}
)
Step 06
Add the request to the queue object:
queue.add(request)
Final code
Main Activity
package com.example.volleydemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val queue = Volley.newRequestQueue(this)
val request = StringRequest(
Request.Method.GET,
"https://jsonplaceholder.typicode.com/users",
Response.Listener<String> {
findViewById<TextView>(R.id.textview).text = it
},
Response.ErrorListener {Log.d("error", it.toString())}
)
queue.add(request)
}
}
UI
<?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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textview"
/>
</ScrollView>
</LinearLayout>
Thanks!
Top comments (0)