Google Volley Android Implement with Video and Source Code

Google Volley is one of the best library use for making HTTP connections. It makes networking easy, fast and secure. Volley is a library built and maintained by Google and thus it is well known and widely used. For beginners Volley is a very good choose as it is much easier than any other libraries. This article will help to to implement Google Volley Android in your project easily.

Know the best practices on implementing a Splash Screen in Android.

Benefits of using Volley:

  • Automatic scheduling of network requests.
  • Multiple concurrent network connections.
  • Transparent disk and memory response caching with standard HTTP cache coherence.
  • Support for request prioritization.
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
  • Ease of customization, for example, for retry and backoff.
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  • Debugging and tracing tools.

If you wish to know more on Volley check out the official documentation of Volley by Google. Now lets see how to implement Volley in you project.

Steps to Implement Google Volley Android

1. Adding the Gradle dependency for google volley android library in your project

Go to Gradle Scripts, then Gradle (Module:app). After that you just have to copy the line starting with “implementation” inside the dependencies section and click sync now.

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

After completely synchronizing, Google Volley will be added to your android project successfully. You need Internet permission for using Volley, so go to manifest and add permission for Internet.

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

Now lets see how we can implement google volley android with some example or lets see how to make a simple request using Volley.

2. Making a simple request for reading data.

This implementation is for reading data, in the example bellow the url “https://projects.vishnusivadas.com/testing/read.php” will print a message “Success” and this is fetched and shown as a toast message.

RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://projects.vishnusivadas.com/testing/read.php";

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
   new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Toast.makeText(getApplicationContext(), "Response: " + response,Toast.LENGTH_SHORT).show();  
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        textView.setText("That didn't work!");
    }
});

queue.add(stringRequest);

Its that simple to create a read request in Volley. Now lets check the request in which we pass some values along with the request, that is the GET and POST method implementation.

3. Creating a GET request

To demonstrate this I have created a PHP API that receives both GET and POST, and if some data is passed it will print a message “Success” other wise shows “Error”.

<?php
if(isset($_REQUEST['param'])){
    echo "Success";
}
else{
    echo "Error";
}
?>

This is how the PHP API looks like. Now to make a GET request you have to pass the data with in the URL. You can see that after the URL I have added the data to be passes like “?param=value“. You could pass more parameters like “?param1=value&?param2=value“.

RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://projects.vishnusivadas.com/testing/read.php?param=value";

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
   new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Toast.makeText(getApplicationContext(), "Response: " + response,Toast.LENGTH_SHORT).show();  
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        textView.setText("That didn't work!");
    }
});

queue.add(stringRequest);

Making a GET request is easy using the Google Volley Android. Now lets check how to make the POST request.

4. Creating a POST request

For creating a POST request you need to send the parameters like the method bellow. Here in the getParams() you can pass as many as arguments as you need. I have created a Map that contains two string. To add more arguments just use the put(). Like paramV.put(“param1”, “data”); paramV.put(“param2”, “data”); etc.

equestQueue queue = Volley.newRequestQueue(this);
        String url ="https://projects.vishnusivadas.com/testing/write.php";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        textView.setText("Response is: "+ response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                textView.setText("That didn't work!");
            }
        }){
            protected Map<String, String> getParams(){
                Map<String, String> paramV = new HashMap<>();
                paramV.put("param", "abc");
                return paramV;
            }
        };
        queue.add(stringRequest);

Hope you understand how to make POST request, if you have any doubts feel free to leave a comment.

1 thought on “Google Volley Android Implement with Video and Source Code”

Leave a Comment