Android的:如何知道位于Web服务器文件被修改? [英] Android: how to know file located in web server is modified?
问题描述
我解析位于Web服务器XML文件,并存储分析数据数据库。我的应用程序,我使用的数据从数据库中。我需要解析只有当文件被修改,否则没必要解析xml文件。所以,我怎么能知道这个文件被修改?我知道我可以使用如果 - 修改 - 自头。但
我需要的如果 - 修改 - 自头
一些例子
请帮我.......
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.......
推荐答案
由于您是从网络服务器检索您的.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文件,你可以简单地执行从Web服务器HEAD请求,这将返回如果该文件已经修改/修改,或者如果它不存在的HTTP请求。这也是轻量级的,最好的部分是服务器应该已经为你做这个。
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.
修改:重读你的问题,看起来你有同样的想法。这里的code。
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;
}
请参阅:
- <一个href="http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html">HttpURLConnection
- <一个href="http://en.wikipedia.org/wiki/HyperText_Transfer_Protocol#Request_methods">HyperText_Transfer_Protocol的HTTPStatus 304(未修改)
- HttpURLConnection
- HyperText_Transfer_Protocol HttpStatus 304 (Not Modified)
另外,如果您的服务器支持他们,你可以使用的ETag来看看你的文件已被修改。
Alternatively if your server supports them you can use ETags to find out if your file has been modified.
- <一个href="http://www.xpertdeveloper.com/2011/03/last-modified-header-vs-expire-header-vs-etag/">http://www.xpertdeveloper.com/2011/03/last-modified-header-vs-expire-header-vs-etag/
这篇关于Android的:如何知道位于Web服务器文件被修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!