Android implementing Volley using Kotlin

Hey Guys, today I am going to tell you how to implement Network hits using Volley Library in Kotlin. Before getting into this tutorial, I tell you what Volley is.

Android volley is a networking library was introduced to make networking calls much easier, faster without writing tons of code. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.

Volley comes with a lot of features. Some of them are
1. Request queuing and prioritization
2. Effective request cache and memory management
3. Extensibility and customization of the library to our needs
4. Cancelling the requests

1. Creating New Project
In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details (do not forget to check “Include Kotlin support”). When it prompts to select a default activity, select Blank Activity and proceed.

2. Open build.gradle and add volley support by adding this under dependencies section.

implementation 'com.android.volley:volley:1.0.0'

3: Include INTERNET permission inside AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>

Step 5:
i) Create an object of RequestQueue class.

val queue : RequestQueue = Volley.newRequestQueue(this)

ii) Create a JSONObjectRequest with response and error listener.

        val url = "https://jsonplaceholder.typicode.com/users"
        val request =  JsonObjectRequest(Request.Method.GET ,  url, null , {
            response: JSONObject? ->
            Log.e("Response : " , response.toString())
        } , {
            error: VolleyError? ->
            Log.e("Error" , error.toString())
        })

iii) Add your request into the RequestQueue.

queue.add(request)

Complete Code of MainActivity.kt file:

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.AppCompatTextView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import org.json.JSONObject

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        apiHit()
    }

    private fun apiHit() {
        val url = "https://jsonplaceholder.typicode.com/users"
        val queue : RequestQueue = Volley.newRequestQueue(this)
        val request =  JsonObjectRequest(Request.Method.GET ,  url, null , {
            response: JSONObject? ->
            Log.e("Response : " , response.toString())
        } , {
            error: VolleyError? ->
            Log.e("Error" , error.toString())
        })
        queue.add(request)
    }
}

Source Code: https://github.com/iamsonumalik/Android-Volley-kotlin-Example


Posted

in

, ,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us


Recent Post


  • The Role of IT Service Companies in Business Innovation

    In today’s dynamic corporate environment, information technology (IT) has emerged as a vital engine of innovation and growth. IT service providers are crucial in helping with the shift as organizations work to remain competitive and fulfill digital world demands. The relevance of information technology in organizations and the crucial help offered by IT service companies […]

  • How To Build a Caching System Through Redis in Node JS?

    In today’s world, you don’t want your application to be slow. If a website is taking too long to respond there might be a chance the user will not visit your application again and Boosting node js application. In this article, we are going to implement the caching system through Redis in NodeJS. But before […]

  • Revolutionizing Healthcare with Big Data: 5 Impactful Use Cases

    In this blog, we are going to understand Big Data and how it is going to revolutionize the healthcare industry. So, first, let’s know what big data is. So, big data is the large volume of data that is coming from businesses, organizations, and people at a very high speed. Big data contains a variety […]

  • The Rise of No-Code/Low-Code Platforms: Democratizing Software Development

    As we are witnessing a paradigm shift in technology, the way we develop software is also undergoing a significant transformation. Just imagine a world where you can design and develop software applications just as easily as you organize your wardrobe. This is what no-code/low-code platform promises, These platforms offer visual tools and seamless interfaces that […]

  • NLP: A Human Way To Talk With Computers

    In a world where dependency on technology is increasing day by day, our interaction with computers and machines has evolved drastically. It is seen very normally that the humans are talking to the computers in their own language and getting their work done. But haven’t we been taught that computers only understand machine language? Well, […]

  • How Quantum Computing is Transforming Software Development Progress?

    Hello there! In this blog, I’ll outline how quantum computing can be combined with software development to rethink how we have been creating programs up to this point and how it will completely change the software development progress and you’re going to know how quantum computing helps software development progress. But before I discuss how […]