Google released Android Volley Library around May/June 2013, which has been internally used by Google for some time. It is supposed to provide Fast Networking Operations and also takes care of Threads nicely. If you are unfamiliar with Volley, please see Google I/O 2013 Video
Unfortunately, there is almost no documentation on Volley. So, I put together code snippets on how to make Volley HTTP Requests (GET, POST, PUT, DELETE).
Setting up is straight-forward. Clone the Volley project from here and then import the Volley into project. A comprehensive tutorial on setting up can be found here.
The following are the Key classes of Volley:
At first make a RequestQueue, which holds the HTTP Requests. View Source. Ideally, the RequestQueue should be made once and then referred to it across the Application. The Application is an ideal place to make it.
1
|
RequestQueue queue = Volley.newRequestQueue(
this
);
// this = context
|
Making GET Requests is simple. The example below uses JsonObjectRequest. It prepares a JsonObjectRequest and passes and then adds it to RequestQueue. The JsonObject accepts 4 parameters (Http method, Url, Json values, Response Listener - Invoked on success, Error Listener - Invoked on failure).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
final
String url =
"http://httpbin.org/get?param1=hello"
;
// prepare the Request
JsonObjectRequest getRequest =
new
JsonObjectRequest(Request.Method.GET, url,
null
,
new
Response.Listener
{
@Override
public
void
onResponse(JSONObject response) {
// display response
Log.d(
"Response"
, response.toString());
}
},
new
Response.ErrorListener()
{
@Override
public
void
onErrorResponse(VolleyError error) {
Log.d(
"Error.Response"
, response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
|
For a POST request, to add form parameters/values, the getParams() method needs to be overridden and a Map needs to be returned.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
url =
"http://httpbin.org/post"
;
StringRequest postRequest =
new
StringRequest(Request.Method.POST, url,
new
Response.Listener
{
@Override
public
void
onResponse(String response) {
// response
Log.d(
"Response"
, response);
}
},
new
Response.ErrorListener()
{
@Override
public
void
onErrorResponse(VolleyError error) {
// error
Log.d(
"Error.Response"
, response);
}
}
) {
@Override
protected
Map
{
Map
new
HashMap
params.put(
"name"
,
"Alif"
);
params.put(
"domain"
,
"http://itsalif.info"
);
return
params;
}
};
queue.add(postRequest);
|
Creating PUT Request is same as POST basically.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
url =
"http://httpbin.org/put"
;
StringRequest putRequest =
new
StringRequest(Request.Method.PUT, url,
new
Response.Listener
{
@Override
public
void
onResponse(String response) {
// response
Log.d(
"Response"
, response);
}
},
new
Response.ErrorListener()
{
@Override
public
void
onErrorResponse(VolleyError error) {
// error
Log.d(
"Error.Response"
, response);
}
}
) {
@Override
protected
Map
{
Map
new
HashMap
params.put(
"name"
,
"Alif"
);
params.put(
"domain"
,
"http://itsalif.info"
);
return
params;
}
};
queue.add(putRequest);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
url =
"http://httpbin.org/delete"
;
StringRequest dr =
new
StringRequest(Request.Method.DELETE, url,
new
Response.Listener
{
@Override
public
void
onResponse(String response) {
// response
Toast.makeText($
this
, response, Toast.LENGTH_LONG).show();
}
},
new
Response.ErrorListener()
{
@Override
public
void
onErrorResponse(VolleyError error) {
// error.
}
}
);
queue.add(dr);
|
Hope it helps!