Android:如何将sqlite数据与服务器同步 [英] Android: How to synchronize the sqlite data with server

查看:93
本文介绍了Android:如何将sqlite数据与服务器同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android:当互联网不可用时如何使用sqlite数据库存储用户数据,然后在设备连接到Internet时自动将存储的数据与服务器同步(我希望场景如何工作)我的数据将包含字符串和图像,但是我将图像路径而不是图像存储在sqlite数据库中.

Android: how sqlite database could be used to store the user data when internet is not available and then synchronise the stored data automatically with server when device is connected to internet (i want the scenario how it works) my data will contain strings, as well as images, but i will store image path instead of images in sqlite database.

如果任何人都可以用简单的示例代码向我解释,那么我将不胜感激.

if anybody can explain me with simple example code then it will be grateful am just a beginner.

推荐答案

  1. 在您的应用程序中创建一个SQLite数据库,以便将所有信息存储在本地.在这里,您可以获得有关为应用程序创建和维护SQLite数据库的详细教程. SQLite教程

在SQLite表的内部,您可以制作一个类似于 isSynced 的文件,并将其位从0切换为1,这取决于记录是否在服务器上更新.

Inside your SQLite table's you can make a filed like isSynced and toggle it's bit from 0 to 1 depending upon that the record is updated on the server or not.

创建一个可以在应用程序启动时运行的服务,但仅在该服务未运行时对其进行检查.该服务每隔 2分钟 isSynced 位为0的位置扫描数据库的所有表,即那些需要更新到服务器的记录,请确保检查Internet连接,然后扫描数据库,以便它可以同时发送记录.此时,您将获得需要在服务器上更新的记录的列表.现在,在循环中点击您的服务以将记录发送到服务器,并在记录成功保存到服务器上后立即确保将其 isSynced 位更新为1.在这里您可以阅读详细的教程在 Android中的服务

Create a service that you can run on the start of your application but make a check to run that service only if it is not running. That service will scan all the tables of the database at every 2 minutes whose isSynced bit is 0 i.e. those records that need's to be updated to the server, make sure to check the internet connection before scanning the database so that the it can send the records at the same time. At this point you will get a list of the records that need's to be updated on the server. Now hit your service in the loop to send the records to the server and as soon as a record is successfully saved on the server make sure to update it's isSynced bit to 1. Here you can read a detailed tutorial on Service in Android

要检查互联网连接,您可以尝试以下代码:

In-order to check the internet connection you can try the following code:

public static boolean isConnected(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED)
                    return true;
            }
        }
    }
    return false;
}

您还需要在清单文件中添加以下权限:

You also need to add the following permissions in your Manifest file:

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

您还可以查看教程您与网络服务进行通信

You can also check this tutorial that will help you in communicating with the web services

希望这会帮助您实现目标.

Hope this will help so in achieving you Goal.

这篇关于Android:如何将sqlite数据与服务器同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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