Making HTTP Request GET,POST,PUT & DELETE in an android using volley-library.

Hello folks,today we will learn about the volley HTTP requests such as GET,POST,PUT and DELETE step by step with its  programming (code).

So before performing the different operation such GET,POST,PUT and DELETE.We have to first set up the android volley library.

Setting volley-library

For getting more information about the volley library and setting up volley library you can check my previous tutorials where you can set volley library using Volley Singleton class.

Check here:

Let’s start with Android library

First of all we have to set up the RequestQueue( )  which holds the HTTP requests.It can create at once and use it in the whole application.

RequestQueue requestqueue=Volley.newRequestQueue( this);

Note:

This RequestQueue( ) also can be used inside Volley Singleton class and use it.

More details:

Making GET Request

Making GET request in a volley is a simple.The below example uses the StringRequest where it accepts four parameters(HTTP method,URL,JSON value,Response Listener) and Gives success or failure according to the condition.

String url=”https://your_json_url”;
StringRequest stringrequest=new StringRequest(Request.Method,GET,url,new Response.Listener<String>( )
{
@Override
Public void onResponse(String response)
{
}

},
new Response.ErrorListener( )
{
@Override
Public void onErrorResponse(VolleyError error)
{
}
});

requestqueue.add(stringrequest);

Making POST Request

Making POST request in a volley.We have to add a parameter in the form of 

key/value pair inside the getParam( ) method and return Map.

String url=”https://your_json_url”;
StringRequest stringrequest=new StringRequest(Request.Method,POST,url,new Response.Listener<String>( )
{
@Override
Public void onResponse(String response)
{
}

},
new Response.ErrorListener( )
{
@Override
Public void onErrorResponse(VolleyError error)
{
}
}
{
@Override
Protected Map<String,String> getParams( )
{
Map<String,String> map=new HashMap<String,String>( );
map.put(“name”,”suman kumar”);
map.put(“website”,”placement”);
return map;
}
});

requestqueue.add(stringrequest);

Making PUT Request

PUT Request is same as POST Request.This is generally used  for updating the data.

String url=”https://your_json_url”;
StringRequest stringrequest=new StringRequest(Request.Method,PUT,url,new Response.Listener<String>( )
{
@Override
Public void onResponse(String response)
{
}

},
new Response.ErrorListener( )
{
@Override
Public void onErrorResponse(VolleyError error)
{
}
}
{
@Override
Protected Map<String,String> getParams( )
{
Map<String,String> map=new HashMap<String,String>( );
map.put(“name”,”suman kumar”);
map.put(“update”,”placement preparation”);
return map;
}
});

requestqueue.add(stringrequest);

Making DELETE Request

DELETE Request is also similar to the above request.It is used to delete the data from the database according to specified condition.

String url=”https://your_json_url”;
StringRequest stringrequest=new StringRequest(Request.Method,DELETE,url,new Response.Listener<String>( )
{
@Override
Public void onResponse(String response)
{
}

},
new Response.ErrorListener( )
{
@Override
Public void onErrorResponse(VolleyError error)
{
}
}
{
@Override
Protected Map<String,String> getParams( )
{
Map<String,String> map=new HashMap<String,String>( );
map.put(“name_id”,”suman kumar”);
return map;
}
});

requestqueue.add(stringrequest);

Hope,I explained all the HTTP Request step by step.If you still facing any problems you can comment me.

Thanks and regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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