Marija Milosevic
Marija Milosevic

Reputation: 516

Volley - more than one request in the same Activity android

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

Upvotes: 1

Views: 2452

Answers (1)

Shooky
Shooky

Reputation: 1299

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
        }
  });

Upvotes: 3

Related Questions