I'm using volley and I have a queue to call some APIs. The queue is filled from a database.
before adding request to volley
request queue I set request tag by calling
jsonObjectRequest.setTag(id);
In response, I want to remove a column from the database
that column id is equal to request tag id.
So, How can I get request tag in HttpRequest
response
?
Use newRequestQueue RequestQueue queue = Volley. newRequestQueue(this); String url = "https://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request. Method.
A volley class used for blocking requests. To access the get() method and make an Android Volley synchronous request, you need to use the RequestFuture class instead of standard StringRequest or JsonObjectRequest class. This RequestFuture class also implements both Response. Listener and Response.
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network connections.
First create a Listener that give response from your volly class
/** Callback interface for delivering parsed responses. */
public interface Listener {
/** Called when a response is received. */
public void onResponse(Object tag, JSONObject response);
public void onErrorResponse(Object tag, VolleyError error);
}
And now create method as below where you pass listener and tag and call volly request. in response you able get tag and response at same time.
public void callApi(String url, final Listener listener, final Object tag){
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listener.onResponse(tag,response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onErrorResponse(tag,error);
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
Its just sample code, You can modify on your requirement. If you need any help comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With