google / volley

https://google.github.io/volley
Apache License 2.0
3.37k stars 751 forks source link

Volley 1.2.0 listener calls multiple times #409

Closed VirajShirwalkar closed 3 years ago

VirajShirwalkar commented 3 years ago

I used volley 1.2.0 and found that for one get api call, there is a listener which gets called multiple time and dont know why.

 requestQueue.addRequestEventListener((request, event) -> {
    Log.i(TAG, "----- addRequestEventListener -----");
   if (request.hasHadResponseDelivered() && event == RequestQueue.RequestEvent.REQUEST_FINISHED){
       // do your work here...
   }
});

please help me to clear this. (BTW i dont remove that listener anywhere in my code. So adding same listener multiple times may create problems. image NetworkGetCall is just a class to call volley get method. )

jpd236 commented 3 years ago

RequestEventListener is for listening to various events over the course of executing a request. It's intended for debugging/logging purposes rather than meant to be what you actually use to listen to responses for a particular API call.

Why are you using this interface, as opposed to passing in a listener to the Request constructor (which would only be invoked once - unless the request is using soft caching)?

VirajShirwalkar commented 3 years ago

Thank you for the reply. But I am not satisfied with the answer. There is no proper or simple example of this version library (1.2.0) available anywhere to understand what are the changes and how to use them. There are two places where response gets received. like below,

StringRequest stringRequest = new StringRequest(apiUrl, response -> {  

and

@Override
  protected void deliverResponse(String response) {
     super.deliverResponse(response);
}

Now, where to collect the response? where to finish the final process to navigate back to Activity / Fragment? Please help.

jpd236 commented 3 years ago

There is documentation and a simple example of how to make requests at https://developer.android.com/training/volley. Nothing significant has changed in version 1.2.0 regarding how to use the library for basic requests compared to older versions.

The first callback you mentioned is the one provided by the code making the request. That would typically be where you'd act on the results, or pass the results to an interested Activity/Fragment.

The second is only needed when you're implementing a custom subclass of Request - see https://developer.android.com/training/volley/request-custom#deliverresponse.

I'm going ahead and closing this out as this seems more like a question of how to use the library than an issue report. If you have further questions about how to use the library, I'd recommend using a general help forum like our user group at volley-users@googlegroups.com, or a site like StackOverflow. We do have an open bug (#395) to flesh out the docs a bit further and hopefully make them more useful.