get Last Modified Date By Url - Android Network

Android examples for Network:HTTP Get

Description

get Last Modified Date By Url

Demo Code


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main{
    //from w w  w  . j a va 2  s .c om
    public static String getLastModifiedByUrl(String url) {
        String value = null;
        URL cURL = null;
        try {
            cURL = new URL(url);
            URLConnection connection = cURL.openConnection();
            // ????????
            connection.setDoOutput(true);
            connection.addRequestProperty("Connection", "close");
            connection
                    .addRequestProperty(
                            "User-Agent",
                            "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4");
            connection.setConnectTimeout(5 * 1000);
            connection.setReadTimeout(10 * 1000);
            connection.connect();

            String time = connection.getHeaderField("Last-Modified");
            Date date = DateUtils.parserDate(time);
            value = DateUtils.formatDate(date,
                    DateUtils.FULL_STANDARD_PATTERN2);
            return value;
        } catch (Exception e) {
            System.err.println("ERROR:" + e);
        }
        return null;
    }
}

Related Tutorials