Android Networking with Volley
Tags: android, volley, kotlin, networking
Published: 2022-12-07 12:33:45

## Introduction

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. It is available on [GitHub](https://github.com/google/volley).

## 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](https://github.com/SajjadAli54/android-material/tree/main/Projects/networking-with-volley)


![Process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbpbmdxtc4ue4fd3d7jz.jpg)

### 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:

```xml
<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.

```gradle
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:

```kotlin
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:

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

```kotlin
 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:

```kotlin
 queue.add(request)
```

## Final code

### Main Activity

```kotlin
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
<?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!**

![Thanks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jhpfpc946bvdlhpp7ff.jpg)