如何从服务器获取数据并用它更新数据库?
我正在实现一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有一种更聪明的方法可以做到这一点,但它需要在服务器端和客户端上进行一些工作。它的工作原理是这样的:所有数据都应该可以 JSON 或 XML 格式下载(我更喜欢 JSON)。因此,您将在服务器中看到类似的内容: http://example.com/resource.json 。为了知道是否有新版本的数据,您可以在 HTTP 响应中添加标头版本(这样您就不必下载并解析整个资源来知道是否有新版本)。
为了让您检查这些标头,您将看到如下内容:
下载和解析 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:
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.