android volley caching asynchronous requests - How te get url of the request in ResponseListener

℡╲_俬逩灬. 提交于 2020-01-17 02:40:09

问题


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


                }

            } 
        }
    }

回答1:


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) {


    }
}



回答2:


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


                }

            } 
        }
    }



回答3:


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) {
         //...
       }
    }
);


来源:https://stackoverflow.com/questions/21433859/android-volley-caching-asynchronous-requests-how-te-get-url-of-the-request-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!