1

I want to send two different requests and handle two different responses in one Activity using Volley library. My activity implements onResponseListener, so i have only one onResponse method and both responses are handled here. As they are completely same in structure i cant tell which is which.

How can i tell from which request i have received the response so i can handle them differently? Is there a way to "tag" a request or something like that?

I could set some kind of check variable, e.g. boolean firstRequestIsSent when i send the request, and then check it in the onResponse method, but its a pretty ugly solution.

Many thanks

Marija Milosevic
  • 516
  • 4
  • 16
  • 1
    just instantiate the callback in the constructor of the request class, or you can write a wrapper around the volley :) but FYI there were a few rest api libraries that worked in centralized way that you want it. You had to add and ID for the call.. and google possible thought about this and there is a reason why is volley not like that (sry for bad english not my native) – cesztoszule Feb 10 '15 at 14:14
  • http://stackoverflow.com/questions/17962904/volley-library-for-android-parse-xml-response – Ajay Pandya Feb 13 '15 at 09:09

1 Answers1

3

Instead of implementing onResponse as part of the class, you can instantiate a new Response.Listener with the request. This way you will have a separate listener for each request.

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener() {
            @Override
            public void onResponse(String response) {
                 // individual response here
            }
        }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // error here
        }
  });
Shooky
  • 1,269
  • 8
  • 16
  • Thanks, this solves it, but i have made a generic class for requests that handles a lot of stuff, so i'd really like to try to "tag" the request somehow so i can keep this implementatiton. Do you have any idea how to identify the request in the onResponse method? – Marija Milosevic Feb 10 '15 at 14:17
  • 1
    the request tag is only for canceling the actual request. Only way to determine which request called onResponse is by checking the data that was returned and matching it to a call. – Shooky Feb 10 '15 at 14:48