带有Volley的Android离线请求 [英] Android Offline Request with Volley

查看:58
本文介绍了带有Volley的Android离线请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的用户提供更好的脱机用户体验,因此,我想构建一个存储所有POSTDELETEPUTService(GET毫无意义,因为没有网络的GET呼叫是一个缓存呼叫),要求用户脱机并在用户建立Internet连接后立即将其发送到服务器.我希望它具有持久性:即使应用程序被杀死,也要发送数据,以免缓存和服务器数据之间出现不一致之处.

I want to give to my user a much better offline user experience, therefore, I want to build a Service which stores all POST, DELETE, PUT (GET makes no sense because a GET call without network is a cache call) requests the user does offline and send them to the server as soon as the user got an internet connection. I want it to be persistent: even if the app is killed, data are sent in order not to have inconstancies between cache and server data.

我对Google Volley和Android Networking API非常熟悉->我知道如何检测到没有网络,如何预取数据,将其缓存等...

I'm quite familiar with Google Volley and Android Networking API --> I know how to detect there is no network, how to prefetch data, to cache them etc...

但是关于这个主题是否有要点或图书馆?我知道最新的Facebook版本实现了这样的系统,但我想知道它们是如何做到的(我的意思是,我知道这里使用的是Service,但是他们到底是怎么做到的,不知道!).有人对此有想法吗?有经验吗?

But is there a gist or a library about this subject? I know that the latest Facebook version implements such a system but I wonder how they did (I mean, I know there're using a Service but how they exactly did, got no idea!). Does someone has idea on that, any experience?

推荐答案

您需要使用BroadcastReceiver来侦听网络更改事件.通过以下操作在AndroidManifest.xml中定义一个broadcastReciver.

You need to use a BroadcastReceiver to listen to network change events. Define a broadcastReciver in your AndroidManifest.xml with the following action.

<receiver android:name=".NetworkBroadcastReceiver" >
     <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
     </intent-filter>
</receiver>

还要向清单文件中添加以下权限-

Also add the following permissions as well to your manifest file -

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

NetworkBroadcastReceiver-

NetworkBroadcastReceiver -

public class NetworkBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {

        if(isInternetConnected(context)) {
            //Do something i.e. trigger an API call or start a IntentService etc.
        }    
    }


    public boolean isInternetConnected(Context context) {
        ConnectivityManager connectivityManager 
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }    
}

这是您在获得互联网后立即发出请求所要做的全部事情.

That's all you need to do to make a request as soon as you get Internet.

要缓存数据,我建议您解析服务器响应并将其保留在数据库中以供脱机使用.每次成功请求时,将新数据保存在数据库中并丢弃旧数据.当用户启动应用程序时,首先从数据库中加载数据,然后触发Volley请求,如果请求成功,则将新数据加载到应用程序中,将其存储在数据库中并摆脱旧数据.因此,如果请求失败,用户仍然可以查看先前成功请求中的旧数据.

To cache data, I'll advise you to parse the server response and keep it in your database for offline use. Every time you make a successful request save the new data in database and discard the old one. When user starts the app, load the data from database first and then trigger the Volley request, if request become successful then load the new data in app, store it in database and get rid of the old one. So in case if request fails, user will still be able to see the old data from previous successful request.

要处理应用程序和服务器中的数据不一致,您需要使用SyncAdapter. SyncAdapter为在后台进行定期同步提供了强大的支持.只需将同步代码放入SyncAdapter的onPerformSync()方法即可.在以下两种情况下可能无法正常工作- 1.如果用户未连接到互联网 2.如果用户设备已关闭

To handle inconsistency between data in app and server, you need to use SyncAdapter. SyncAdapter provide a great support for periodic sync in the background. Just put the syncing code in onPerformSync() method of SyncAdapter. It might not work in following two scenarios - 1. If user isn't connected to internet 2. If user device is turned off

要处理这些情况,请使用我在答案中上面解释的BroadCastReceiver来触发SyncAdapter.还要在AndroidManifest.xml的接收器中添加以下操作,以侦听设备的启动完成事件.

To handle these scenario, use the BroadCastReceiver that I explained above in my answer to trigger the SyncAdapter. Also add the following actions to your receiver inside AndroidManifest.xml to listen to boot complete event of device.

<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />

让我知道您是否需要使用编码示例进行更深入的说明.希望对您有帮助

Let me know if you need more in depth explanation with coding example. Hope it helps

这篇关于带有Volley的Android离线请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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