Android volley is a networking library was introduced to make networking calls much easier, faster without writing tons of code. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.
Volley offers the following benefits:
- 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.
Following are the important class of volley:
- RequestQueue: A Queue containing the Network/HTTP Requests that needs to be made.
- Request: A Base Class which contains Network related information like HTTP Methods.
- StringRequest: HTTP Request where the response is parsed a String.
- JsonObjectRequest: HTTP Request where the response is JSONObject.
To use Volley, you must add the
Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "Url goes here"
android.permission.INTERNET
permission to your app's manifest. Without this, your app won't be able to connect to the network. Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "Url goes here"
Making GET Requests
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
//error
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);
Making POST Requests
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@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<String, String> getParams()
{ // Adding parameters
Map<String, String> params = new HashMap<String, String>();
params.put("parameter1", "value1");
params.put("parameter2", "value2");
return params;
}
};
queue.add(postRequest);
Making PUT Requests
StringRequest putRequest = new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() { @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<String, String> getParams() { Map<String, String> params = new HashMap<String, String> (); params.put("parameter1", "value1"); params.put("parameter2", "value2"); return params; } }; queue.add(putRequest);
Making DELETE Requests
StringRequest deleteRequest = new StringRequest(Request.Method.DELETE, url,
new Response.Listener<String>()
{
@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);
}
}
);
queue.add(deleteRequest);
No comments:
Post a Comment