4

Android Networking with Volley

 1 year ago
source link: https://dev.to/sajjadali/android-networking-with-volley-coi?signin=true
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. It is available on GitHub.

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"/>

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:

  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())}
 )

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)
    }
}
<?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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK