5

I've used setTags when making a google volley request. How do you use getTag or other method in the in-line onResponse or a response listener?

        public boolean VolleyRequestWrapper(final int which, String url, final String volleyTag) {

             JsonObjectRequest jsonObjReq = null;


         jsonObjReq = new JsonObjectRequest(
                Method.GET,
                url, 
                null,
                new Response.Listener<JSONObject>() {
                     @Override
                    public void onResponse(JSONObject response) {

                        ??????.getTag();   // <-----  THIS IS WHERE I WANT TO get the Tag.


                    }
                }, new Response.ErrorListener() {
                     public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(volleyTag, "Error: " + error.getMessage());
                        volleyException = true;
                    }
                });

        // Adding request to request queue
        jsonObjReq.setTag(volleyTag);  // I can set the tag
        requestQueue.add(jsonObjReq);

        return !volleyException;
    }

I've looked but haven't found a good example that uses getTag and I haven't found an documentation on volley. Any help would be appreciated.

Have a great day.

Peter Birdsall
  • 3,159
  • 5
  • 31
  • 48
  • The purpose of the tag value is to cancel out those ongoing request explicitly when needed. – KunalK Jun 19 '14 at 11:22
  • I thought that one of the things that volley does well is handle multiple network requests for small file, so then hoe do you differentiate between several requests? In addition at the end of twistedequations on custom volley requests is mentioned handling multiple requests, but doesn't show how. So do you know how to differentiate between requests? – Peter Birdsall Jun 19 '14 at 12:45
  • check this http://pastebin.com/NSmik7x4 – KunalK Jun 19 '14 at 12:51
  • Thanks, but I already know how to cancel volley requests. Have a great day. – Peter Birdsall Jun 19 '14 at 13:25

2 Answers2

0

I tried to explore it and it seems there isn't any way to achieve the mechanism to differentiate between multiple tagged requests, it seems to be the limitation of Volly, theres alternate solution to use IntentService instead of Volly containing HTTP code and providing tag in response object obtained from your network rrequest

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
0

The tag you assign to a Request is stored to the variable mTag which is persisted throughout the life cycle of the Request.

For my applications I use a slightly modified the Volley classes to send it back along with a Response.

/** Callback interface for delivering parsed responses. */
public interface Listener<T> {
    /** Called when a response is received. */
    public void onResponse(Object tag, T response);
}

Please see my answer https://stackoverflow.com/a/37129734/4419474 for a similar question for details.

Community
  • 1
  • 1
Chebyr
  • 2,171
  • 1
  • 20
  • 30