Android:如何知道位于 Web 服务器中的文件已被修改? [英] Android: how to know file located in web server is modified?

查看:21
本文介绍了Android:如何知道位于 Web 服务器中的文件已被修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析位于 Web 服务器中的 Xml 文件并将解析后的数据存储到数据库中.对于我的应用程序,我使用的是数据库中的数据.仅当文件被修改时我才需要解析 xml 文件,否则不需要解析.那么我怎么知道文件被修改了呢?我知道我可以使用if-modified-since"标题.但我需要一些if-modified-since"标题的例子
请帮帮我......

I am parsing Xml file located in web server and storing parsed data in to database. for my app I am using data from database. I need to parse the xml file only if the file is modified otherwise no need to parse. So how can I know the file is modified? I know I can use "if-modified-since" header. But I need some examples of "if-modified-since" header
please help me.......

推荐答案

由于您是从 Web 服务器检索 .xml 文件,因此这应该相对容易,而无需进行服务器端 MD5 求和.

Since you are retrieving your .xml file from a web server, this should be relatively easy without having to do a server side MD5 sum.

如果您对 xml 文件执行 HTTP 请求,您可以简单地从 Web 服务器执行 HEAD 请求,如果文件已更改/修改或不存在,它将返回.这也是轻量级的,最好的部分是服务器应该已经为你做这件事了.

If you are doing a HTTP request for the xml file you can simply perform a HEAD request from the web server and this will return if the file has changed/modified or if it doesn't exist. This is also lightweight and the best part is that the server should already do this for you.

编辑:重新阅读您的问题,看起来您有相同的想法.这是代码.

Edit: re-reading your question, looks like you had the same idea. Here's the code.

import java.net.*;
import java.io.*;

// Using HTTP_NOT_MODIFIED
public static boolean Changed(String url){
    try {
      HttpURLConnection.setFollowRedirects(false);
      HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED);
    }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
  }

// GET THE LAST MODIFIED TIME
public static long LastModified(String url)
{
  HttpURLConnection.setFollowRedirects(false);
  HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
  long date = con.getLastModified();

  if (date == 0)
    System.out.println("No last-modified information.");
  else
    System.out.println("Last-Modified: " + new Date(date));

  return date;
}

见:

或者,如果您的服务器支持它们,您可以使用 ETags 来确定您的文件是否已被修改.

Alternatively if your server supports them you can use ETags to find out if your file has been modified.

这篇关于Android:如何知道位于 Web 服务器中的文件已被修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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