Android volley tutorial

If we want to make a network call we need to implement an Asynctask to avoid NetworkOnMainThreadExeption and of the code will also be complex with multiple functions in which each functions are having some limitations. By using android volley library we can reduce the complexity of code and make the network calls easily.

In this tutorial we will learn how to make a simple volley network call which will help beginners to start using volley library.

Steps

  1. Compile volley library in build.gradle file
  2. Implement code for network calls.
  3. How to upload files using volley library

1.Compile volley library in build.grade

For making a network call using volley you need to have the volley library in your project. For getting the volley library in your project you just need to add the following library in your build.gradle file and sync the project.Please not that you should always use the latest version.

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

2.Implement code for network calls

Once you synced your library you can now start making volley request. You can make network calls using volley library using a small set of code below. Please note that you need to replace the url with the url which you want to request in the following code

RequestQueue queue = Volley.newRequestQueue(this);
//replace your url in the following line
String url = "your url here";

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String res = response;
Toast.makeText(getApplicationContext(),""+response,Toast.LENGTH_SHORT).show();

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//failed
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
finish();
}
}) {
//adding parameters to the request
@Override
protected Map<String, String> getParams() throws AuthFailureError
 {
Map<String, String> params = new HashMap<>();
params.put("param_key","param_value"); 
return params; 
} 
}; 
// Add the request to the RequestQueue. 
queue.add(stringRequest);

The above code is for making a post request . The response of the call will be in string . Suppose if your response of the call is a json string you can place the entire response in a json object and parse it. Now in next step we will learn how to upload an image or file using volley library in android

3.How to upload files using volley library in android

For uploading files using volley library let us do a library update . Let us replace the above added library with following which will provide you some extra features.

implementation 'dev.dworks.libs:volleyplus:+'

Once you sync the library you can use the following code to upload an image or a file using volley. For uploading an image or file first you need to get the selected file from the uri . After having the path you can use the following code.

String url = "Your api link";
String path = "Your File Path";
SimpleMultiPartRequest req = new SimpleMultiPartRequest(Request.Method.POST, url,
new Response.Listener<String>() { 
@Override 
public void onResponse(String response) 
{ 
Log.d("Response", response); 
} 
}, new Response.ErrorListener() { 
@Override 
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}); 
req.addStringParam("string_key", "Sting_value");
req.addFile("file", path);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(req);

Note that you need to replace the upload file path and your api link in the above code to work. And you can use it in a button click or any other action based on your requirement.

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

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