android volley caching asynchronous requests - How

2019-08-29 04:21发布

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


                }

            } 
        }
    }

3条回答
2楼-- · 2019-08-29 05:06

if you create new listener instances on each request you're unique:

StringRequest request = new StringRequest(Request.Method.GET, url, null, 
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
          //...
        }
    }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
         //...
       }
    }
);
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-29 05:09

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.

private class ResponseListenerYYY extends Api.ResponseListener {
    @Override
    public void onResponse(String response, boolean changed) {

    }

    @Override
    public void onErrorResponse(VolleyError error, String response) {


    }
}
查看更多
爷、活的狠高调
4楼-- · 2019-08-29 05:16

You can create a private variable inside your Listener and then reference it later:

Application.get().getApi().getRequest(url, new ResponseListenerX(url));

private class ResponseListenerX extends Api.ResponseListener {
        private String requestedUrl;

        public ResponseListenerX(String requestedUrl) {
            this.requestedUrl = requestedUrl;
        }

        @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(requestedUrl);

                if (response != null && response.length() > 0) {
                    // onResponse


                }

            } 
        }
    }
查看更多
登录 后发表回答