DEV Community

Cover image for Android Networking with Volley
Sajjad Ali
Sajjad Ali

Posted on • Updated on

Android Networking with Volley

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

Process

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"/>
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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:

  1. JsonObjectRequest
  2. JsonArrayRequest
  3. ImageRequest
  4. StringRequest

We would be working with StringRequest. It requires four things from us:

  1. Method: GET, POST, etc.
  2. Source link
  3. Listener for success
  4. 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())}
 )
Enter fullscreen mode Exit fullscreen mode

Step 06

Add the request to the queue object:

 queue.add(request)
Enter fullscreen mode Exit fullscreen mode

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Thanks!

Thanks

Top comments (0)