Introduction:
Volley is a Google API that allows you to build requests to REST APIs from an Android phone.
It can be used, for example, to implement a connection system to a database hosted on a remote server.
In this tutorial, we will cover the basics of the REST API, the different features offered by Volley, and also the basics of connecting to the REST API and how to connect to it with Android.

Basics of REST API:
You can think of the REST API as an interface on top of your server that accepts connections and executes commands based on your needs. For example, you can send a GET request to your server to retrieve data.
If you have multiple users for your application and you want to allow them to access shared features (such as viewing another person’s profile in a game app), you can consider using a REST API, and therefore Volley. You can also choose not to use Volley, but configuring the connection and requests would be more complex to implement.
The REST API allows for several operations:
- GET requests to retrieve existing data from the server. You can use this type of request, for example, to retrieve the leaderboard of players in an online game.
- POST requests to add data to the server. This request can be used, for example, to create a user account.
- PUT requests to modify data on the server. This request can be used to modify a password or username, for example.
- DELETE requests to delete data from the server.
We will cover the implementation of the REST API in more detail in a future article.
Implementing Volley
We will implement the different requests described in the above paragraph using Volley.
Let’s assume that the server we are communicating with contains a list of Users that we want to view and manipulate. Our app will implement the following functionalities:
- Retrieve the list of all users (GET request).
- Retrieve a user sorted by ID (GET request).
- Add a user (POST request).
- Modify the username of a user (PUT request).
- Delete a user by ID (DELETE request)
Let’s start by adding Volley to our App. To do this, you need to add the following line to the application’s Gradle file, in the dependencies section:
implementation 'com.android.volley:volley:1.2.1'
After syncing the Gradle, we can use Volley in our app. We also need to add the following two lines to the Manifest:<uses-permission android:name=”android.permission.INTERNET” /> <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
Additionally, if the website/API we are connecting to does not support HTTPS, we need to add the following line:
android:usesCleartextTraffic="true"
Please be cautious with this option, as it is disabled by default for security reasons. It is preferable to use it for a local network (e.g., internally developed API) to avoid compromising the security of our app.
We will demonstrate the usage of several types of requests: StringRequest, JsonArrayRequest, and JsonObjectRequest.
Preparing the RequestQueue:
To begin, we will create the RequestQueue that allows us to send consecutive requests to the server:
RequestQueue requestQueue = Volley.newRequestQueue(this);
GET Request:
For the GET request, we will perform a Volley request of type JSONArrayRequest. We will send a request to the server, which, if successful, will send us back an array of JSON objects.
public JsonArrayRequest(int method, String url, JSONArray jsonRequest, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener);
In its simplest form, the request is made through a URL. In the case of our small REST API, the URL is {localhost}/GETUSERS:
public void getRequest() { JsonArrayRequest GETRequest = new JsonArrayRequest(Request.Method.GET, url_GET, null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { Log.i("Get userlist", "" + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Deal with error here } });
requestQueue.add(GETRequest);
}
We construct this request as follows:
- Request type: Method.GET.
- The request URL, which here allows us to pass the path “/GETUSERS”.
- Response.Listener and Response.ErrorListener, which wait for the server’s response (through a JSONArray or a Volley error).
- The parameter null corresponds to the JsonArray we pass to the request. Since we are making a general GET request, it does not have additional parameters.
Then we add the request to the queue. It will be executed as soon as the previous requests finish.
POST/PUT Request:
For the POST or PUT request, we will use a JsonObjectRequest:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i("Get userlist", "" + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO: Handle error } });
In the case of a request like this, it can be interesting to send a JSON object, headers, or params to the server so that it can process the POST/PUT request.
To handle this, we modify the request as follows:
JSONObject user = new JSONObject(); user.put("username", "Ahab"); user.put("password", 1234); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, user, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i("Get userlist", "" + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO: Handle error } });
The above request will send a POST with a user as a JSON object payload. Additionally, the Volley methods can be modified as follows to include params and headers:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i("Get userlist", "" + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO: Handle error } }) {
@Override
protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("username", "Ahab"); params.put("password", 1234); return params; }
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
headers.put("Authorization", "Bearer f038738c-5206-42d7-8ade-9c5f52b90681");
return super.getHeaders();
}
};
Params and headers are additional information that can be provided with a Volley request. Headers can add extra information, especially regarding authentication, and params can be an alternative to sending JSON to the server.
DELETE Request:
To demonstrate the other major type of Volley request, we perform a StringRequest for the DELETE:
StringRequest stringRequest = new StringRequest(Request.Method.DELETE, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // Handle response here } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Handle error here } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put("user", userToDelete); return params; } };
The DELETE request allows us to delete a user from the server, here by passing their ID in the params.
This method does not require sending a JSONObject or JSONArray and could potentially use a URL indicating the user to delete instead…
Conclusion:
In this article, we explored the usage of the Volley API to make REST requests from an Android phone. We saw how Volley can be used to perform operations such as data retrieval, user addition, data modification, and user deletion.
The REST API provides a convenient interface for communicating with a server, using requests such as GET, POST, PUT, and DELETE. Volley simplifies the implementation of these requests by providing ready-to-use classes and methods, saving time and avoiding the need to manually manage connection and request configuration.
We learned how to use Volley to perform different types of requests, including StringRequest for simple requests, JsonArrayRequest for retrieving JSON arrays, and JsonObjectRequest for sending JSON objects. We also explored the option of adding params and headers to requests for more advanced customization.
Regarding user deletion from the server, we noted that it is possible to achieve this by simply passing the user’s ID in the DELETE request’s params. This offers a straightforward and efficient approach to deleting users without having to send complex data structures.
About us and AI:

Leave a comment