FarshidABZ
FarshidABZ

Reputation: 4123

Get volley request tag in response

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?

Upvotes: 9

Views: 2063

Answers (1)

Lokesh
Lokesh

Reputation: 3334

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.

Upvotes: 5

Related Questions