Real world Android applications typically need to communicate with one or more backend REST API web services. For the newbie developer, the options – OkHTTP, AsyncTask, Retrofit etc. – can be so overwhelming that it can make it difficult to know where to start. When this happens it’s always worth looking at what Android offers out of the box. Which brings us to Volley, Android’s library for making API calls.

There seems to be a lack of information on how to make a POST request using Volley, especially in Kotlin. So in this blog we’re going to look at using Volley to make a simple GET and POST request to our API.

Setup for Android Volley

You can quickly and easily set up a mock REST API on apiary.io. The following code creates a GET and POST request and server response which we can then call using Volley:-

# The Simplest API
# /message
## GET 
+ Response 200 (text/plain)
        Hello World!
## POST [POST]
+ Request (application/json)
        { 
            "id" : 1,
            "msg": "test msg" 
        }
+ Response 201 (application/json)
        { "id": 1}

Now create a new Empty Activity Project in Android Studio. First add the volley dependency to build.gradle (Module: app) and sync.

dependencies {
    …..
    implementation 'com.android.volley:volley:1.1.1’
}

Now add the following permission to AndroidManifest.xml just above the application node so our app can make the call.

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

GET Volley Commands

Create a new Kotlin function called getVolley(). Make a GET request as follows to our mock server url. stringReq is defined using a closure and we then add that to the Volley queue. Send the expected output “Hello World!” or any error response to the logs.

fun getVolley(){  
    // Instantiate the RequestQueue.  
    val queue = Volley.newRequestQueue(this)  
    val url: String = "https://private-4c0e8-simplestapi3.apiary-mock.com/message"  
  
    // Request a string response from the provided URL.  
    val stringReq = StringRequest(Request.Method.GET, url,  
        Response.Listener<String> { response ->  
		      var strResp = response.toString()  
              Log.d("API", strResp)  
        },  
        Response.ErrorListener {Log.d("API", "that didn't work") })  
    queue.add(stringReq)  
}

We see the expected output in the Android Studio logs, showing that everything is working as planned.

2020-05-05 13:26:31.120 6250-6250/com.riis.volley D/RESPONSE: Hello World!

POST Volley Commands

In this example, we’re passing the parameters id and msg parameters in the Body of the request. So we need to create the requestBody and then pass it to the request by overriding the getBody() function of the Request in the queue.

fun postVolley() {  
    val queue = Volley.newRequestQueue(this)  
    val url = "https://private-4c0e8-simplestapi3.apiary-mock.com/message"  

  val requestBody = "id=1" + "&msg=test_msg"  
  val stringReq : StringRequest =  
    object : StringRequest(Method.POST, url,  
        Response.Listener { response ->  
			 // response  
			 var strResp = response.toString()  
             Log.d("API", strResp)  
        },  
        Response.ErrorListener { error ->  
			 Log.d("API", "error => $error")  
        }  
	){  
    override fun getBody(): ByteArray {  
         return requestBody.toByteArray(Charset.defaultCharset())  
     }  
    }  
  queue.add(stringReq)  
}

Once again we’re sending the response to the logs. We see the expected result as follows.

7004-7004/com.riis.volley D/API: { "id": 1}

Android Volley Conclusion

Sending requests to an API is now a straightforward call using Volley.