Android的凌空缓存异步请求 - 如何TE获得ResponseListener请求的URL [英] android volley caching asynchronous requests - How te get url of the request in ResponseListener

查看:167
本文介绍了Android的凌空缓存异步请求 - 如何TE获得ResponseListener请求的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用了很多凌空做字符串的请求。在每个模块有不同的onResponseListener总是在做不同的东西。而我需要让他们也脱机工作。所以,我正在从缓存的响应。问题是,可以有很多不同的网址异步调用的请求。问题是,如何从缓存中得到,当它记住请求的URL。目前,它只有在全局变量,但请求可以被异步发送,响应可能无法使用全局变量的URL匹配。是否有某种方式如何让onResponse这个请求的原始请求的URL,并直接在Application.get()使用它。getApi()。getCache(网址

请求看起来总是这样:

  Application.get()getApi()调用getRequest(URL,mListener)。

mListener:

 私有类ResponseListenerX扩展Api.ResponseListener {
        @覆盖
        公共无效onResponse(字符串响应){
            如果(响应!= NULL){
            }
        }        @覆盖
        公共无效onErrorResponse(VolleyError错误){            如果((错误== NULL || error.networkResponse == NULL)及和放大器;!URL = NULL){                //如何到达这里从URL请求
                串响应= Application.get()getApi()getCache(URL)。。                如果(响应=空&放大器;!&放大器; response.length()大于0){
                    // onResponse
                }            }
        }
    }


解决方案

所以最好的解决方案是使用凌空延伸:eu.the4thfloor.volleyextended
有更新响应监听器,你在哪里得到更多的布尔改变,这意味着,如果反应是在缓存中最后的响应不同。

和对错误的反应有您可以在服务器或网络outtage的情况下,使用更多的参数响应。当然,如果它仅缓存

 私有类ResponseListenerYYY扩展Api.ResponseListener {
    @覆盖
    公共无效onResponse(字符串响应,布尔改变){    }    @覆盖
    公共无效onErrorResponse(VolleyError错误字符串响应){
    }
}

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


                }

            } 
        }
    }

解决方案

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


    }
}

这篇关于Android的凌空缓存异步请求 - 如何TE获得ResponseListener请求的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆