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

发布于 2024-09-24 20:04:17 字数 137 浏览 6 评论 0原文

我正在实现一个android应用程序,我不知道如何解决这个问题:当用户单击更新按钮时,我想连接到我的服务器并检查数据是否有任何更新(如果有),我想从服务器获取数据并更新数据库。我应该使用 xml 结构来检查并从服务器获取更新还是有任何更智能的方法来实现此目的?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-10-01 20:04:17

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

为了让您检查这些标头,您将看到如下内容:

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

下载和解析 JSON 资源是已经处理过的内容,您可以在 Google 和此处找到大量教程和参考资料。

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文