如何从服务器获取数据并使用它更新数据库?

时间:2021-11-10 16:59:20

I am implementing an android application and i can't figure out how to solve this problem : When user clicks to the update button , i want to connect to the my server and check if there is any update on the data , if there is , i want to get data from server and update the database.Should i use xml structure to check and get updates from server or is there any smarter ways to achieve this ?

我正在实现一个Android应用程序,我无法弄清楚如何解决这个问题:当用户点击更新按钮,我想连接到我的服务器,并检查数据是否有任何更新,如果有,我想从服务器获取数据并更新数据库。我应该使用xml结构检查并从服务器获取更新,还是有更聪明的方法来实现这一点?

1 个解决方案

#1


6  

Yes, there's a smarter way to do so, it will require some work on both server and client side though. This is how it works: all data should be available to download in JSON or XML format (I'd rather prefer JSON). So, you will have something like this in the server: http://example.com/resource.json. In order to know if there's a new version of the data, what you can do is adding a header version to the HTTP response (that way you don't have to download and parse the whole resource in order to know if there's a new version).

是的,这是一种更聪明的方法,但它需要在服务器端和客户端上进行一些工作。这是它的工作原理:所有数据都应该以JSON或XML格式下载(我更喜欢JSON)。所以,你将在服务器中有这样的东西:http://example.com/resource.json。为了知道是否有新版本的数据,您可以做的是在HTTP响应中添加标题版本(这样您就不必下载和解析整个资源以了解是否有新版本)。

In order for you to check those headers, you will have something like this:

为了让您检查这些标题,您将得到以下内容:

URLConnection urlConnection = null;
try {
    urlConnection = url.openConnection();
    urlConnection.connect();

    String currentVersionHeader = urlConnection.getHeaderField("your-version-header");
    if( currentVersionHeader == null ) {
        currentVersionHeader = "-1";
    }

    int version = Long.parseLong(currentVersionHeader);

    // then you compare with the old version
    if( oldVersion < version ){
        // download the data
    }
} catch (Exception e) {}

Downloading and parsing a JSON resource is something that has been already treated and you will find a bunch of tutorials and references on Google and here.

下载和解析JSON资源已经得到了处理,你会在Google和这里找到一堆教程和参考资料。

You are not providing details of the server side (is PHP? Java? .NET?), so I also won't give you details of how to implement/add a version header. I'm just explained you what's one the best way to do this kind of things based on my own experience.

您没有提供服务器端的详细信息(是PHP?Java ?. NET?),因此我也不会详细介绍如何实现/添加版本标头。我只是根据自己的经验向你解释了做这类事情的最好方法。

#1


6  

Yes, there's a smarter way to do so, it will require some work on both server and client side though. This is how it works: all data should be available to download in JSON or XML format (I'd rather prefer JSON). So, you will have something like this in the server: http://example.com/resource.json. In order to know if there's a new version of the data, what you can do is adding a header version to the HTTP response (that way you don't have to download and parse the whole resource in order to know if there's a new version).

是的,这是一种更聪明的方法,但它需要在服务器端和客户端上进行一些工作。这是它的工作原理:所有数据都应该以JSON或XML格式下载(我更喜欢JSON)。所以,你将在服务器中有这样的东西:http://example.com/resource.json。为了知道是否有新版本的数据,您可以做的是在HTTP响应中添加标题版本(这样您就不必下载和解析整个资源以了解是否有新版本)。

In order for you to check those headers, you will have something like this:

为了让您检查这些标题,您将得到以下内容:

URLConnection urlConnection = null;
try {
    urlConnection = url.openConnection();
    urlConnection.connect();

    String currentVersionHeader = urlConnection.getHeaderField("your-version-header");
    if( currentVersionHeader == null ) {
        currentVersionHeader = "-1";
    }

    int version = Long.parseLong(currentVersionHeader);

    // then you compare with the old version
    if( oldVersion < version ){
        // download the data
    }
} catch (Exception e) {}

Downloading and parsing a JSON resource is something that has been already treated and you will find a bunch of tutorials and references on Google and here.

下载和解析JSON资源已经得到了处理,你会在Google和这里找到一堆教程和参考资料。

You are not providing details of the server side (is PHP? Java? .NET?), so I also won't give you details of how to implement/add a version header. I'm just explained you what's one the best way to do this kind of things based on my own experience.

您没有提供服务器端的详细信息(是PHP?Java ?. NET?),因此我也不会详细介绍如何实现/添加版本标头。我只是根据自己的经验向你解释了做这类事情的最好方法。