Get response volleyball request tag

I use volley and I have a queue for calling some APIs. The queue is populated from the database.

before adding the request to the volley request queue I set the request tag by calling

jsonObjectRequest.setTag(id);

In response, I want to remove the column from the database , this column id is equal to the id of the request tag.

So how can I get the request tag in the HttpRequest response ?

+8
json android android-volley
source share
1 answer

First create a listener that will give an answer 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 a method as shown below, where you pass the listener and tag and call the volly request. In response, you can get the tag and the answer at the 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); } 

Just a sample code, you can change your requirement. If you need any comment for help.

+4
source share

All Articles