如果没有互联网连接,则加载本地数据 [英] Load local data if internet connection not available

查看:118
本文介绍了如果没有互联网连接,则加载本地数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,可以显示来自外部数据库的数据,因此可以显示多个表的数据.可以使用Internet连接时,一切工作正常(所有数据均来自URL链接,并且已通过凌空解析).但是,当互联网不可用时,如何保存和加载最新数据.

I have an android application that display data from my external Database so from several tables. Everything work fine while internet connection is available (all data come from URL link and they are been parsed with volley). But how can I save and load lastest data when internet is not available.

做到这一点的最佳方法是什么.我是android的新手....

What is the best way to do that. Im new in android....

请帮助.

推荐答案

通常,Volley及其使用的HttpStack允许您无缝缓存响应.但是,这取决于您的答复和要求.这些缓存策略遵循http缓存定义.如果您想让Volley拥有不同的行为,则可以覆盖此部分.基本上,当您创建请求时,您可以覆盖

Normally Volley and also HttpStack it uses allows you to seamlessly cache responses. This however depends on your responses and requests. Those cache strategies obeys http cache definition. If You want to have different behavior for Volley you can just override this part. basically when you create a request you can override

protected Response<String> parseNetworkResponse(NetworkResponse response) {

而不是

return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));

你做

long now = System.currentTimeMillis();
Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
        entry.ttl = now + 30l * 24 * 60 * 60 * 1000;  //kepps cache for 30 days 
//entry.softTtl = now + 30l * 24 * 60 * 60 * 1000;  //will not refresh for 30 days     
        return Response.success(parsed, entry);

基本上每次服务器指定时都会刷新缓存,但是除非更改,否则它将始终返回缓存30天.

Which basically will refresh the cache every time the server specifies so but will always return the cache for 30 days unless changes .

请注意,在这里您可能会收到2个响应或一个响应和1个错误(如果没有网络的话)的回调.第一个是缓存.

Note that here you may receive 2 callbacks in response or in one response and 1 error(in case of no network). the first one will be the cache.

更新:

如果您添加(在上面的示例中已注释):

if you add (which is commented in the example above):

entry.softTtl = now + 30l * 24 * 60 * 60 * 1000;  //will not refresh for 30 days 

这将影响缓存的刷新.在这种情况下,它不会尝试刷新缓存30天.在这种情况下,您将返回1个响应.

this will affect the refresh of the cache. in this case it wont event try to refresh the cache for 30 days. In that case you will return 1 response.

请注意,我永远不会推荐这样的解决方案,因为需要更新缓存,尤其是如果您想根据情况伪造POST请求上的缓存时.

Note that I never would recommend solution like this because cache needs to be updated, especially if you want to fake cache on POST requests as in your case.

接收2个回调并不是真正的问题,因为您可以为用户无缝处理此问题并在需要时更新UI.另外,如果您想获得更多控制权,则可以通过实现

Receiving 2 callbacks is not really an issue as you can handle this seamlesly for the user and update UI when needed. also if you want to have more control you can know which one is from the cache and which one form the network by implementing your

ResponseDelivery

ResponseDelivery

或扩展

ExecutorDelivery

ExecutorDelivery

然后检查参数

mResponse.intermediate

mResponse.intermediate

,然后决定要做什么. ResponseDelivery是调用回调的那个.

and decide what to do then. ResponseDelivery is the one which calls the callbacks.

更新

类似的问题和示例此处

这篇关于如果没有互联网连接,则加载本地数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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