I have in the app a lot of string requests done in volley. In each module there is a different onResponseListener doing always something different. And I need to make them work also offline. So I am getting the response from the cache. Problem is that there can be a lot of requests called asynchronously with different url. Problem is how to remember the url of the request when getting it from cache. Currently it is only in global variable, but as requests can be sent asynchronously, response might not match with global variable url. Is there some way how to get in onResponse the original requested url of this request and directly use it in Application.get().getApi().getCache(url) ?
request looks always like this:
Application.get().getApi().getRequest(url, mListener);
mListener:
private class ResponseListenerX extends Api.ResponseListener {
@Override
public void onResponse(String response) {
if (response != null) {
}
}
@Override
public void onErrorResponse(VolleyError error) {
if ((error == null || error.networkResponse == null) && url != null) {
// how to get here url from the request
String response = Application.get().getApi().getCache(url);
if (response != null && response.length() > 0) {
// onResponse
}
}
}
}
if you create new listener instances on each request you're unique:
So the best solution was to use volley extended: eu.the4thfloor.volleyextended There is updated Response listener, where you get additional boolean changed which means if response is different to last response in cache.
And on error response there is additional parameter response which you can use in case of server or network outtage. Of course if it is cached only.
You can create a private variable inside your Listener and then reference it later: