Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to know file located in web server is modified?

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

like image 545
Prabhu M Avatar asked Jul 14 '11 10:07

Prabhu M


People also ask

Where are Android app files stored?

For normal apps, there are stored in internal memory in /data/app. Some of the encrypted apps, the files are stored in /data/app-private. For apps stored in the external memory, files are stored in /mnt/sdcard/Android/data.

How do I check file type on Android?

To get the data type of a shared file given its content URI, the client app calls ContentResolver. getType() . This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.

Is Android a Web server?

Android smartphone as a Cloud Webserver is a HTTP server written in the PHP language based webserver and its open source package supports android smartphone devices with any android OS which can be installed into a smartphone with the help of . apk file.

How do I access internal storage on Android?

But in most cases, you can see the internal storage of an Android phone: Navigate to My Files to view internal storage as well as SD card and Network storage. Here, tap Internal Storage to see your files and folders. Tap the DCIM folder to view your photos.


1 Answers

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.

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;
}

See:

  • HttpURLConnection
  • HyperText_Transfer_Protocol HttpStatus 304 (Not Modified)

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

  • http://www.xpertdeveloper.com/2011/03/last-modified-header-vs-expire-header-vs-etag/
like image 117
Justin Shield Avatar answered Sep 21 '22 16:09

Justin Shield