Get volley request tag in response
Asked Answered
G

1

9

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?

Groggery answered 7/11, 2017 at 12:44 Comment(4)
This might help you LinkAntalya
@MohammedFarhan So you mean I have to compile volley source code in my application?Groggery
exactly that answer is accepted, so you need to follow it.Antalya
You actually don't have to have the volley source code, you can override the Request class and make your own.Shabuoth
P
5

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.

Photoactinic answered 17/11, 2017 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.