-1

im trying to get the last-modified date from an apk file on my server, the problem as soon as i try to get the header it fails somehow.

i can download the file fine with

try {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection con =  (HttpURLConnection) new URL(params[0]).openConnection();

            con.setRequestMethod("HEAD");
            return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
        } // do some more not relevant

but as soon as i try to get the header from the server it fails

            URL obj = new URL(customURL);
            URLConnection conn = obj.openConnection();
            Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                System.out.println("Key : " + entry.getKey()
                        + " ,Value : " + entry.getValue());
                Toast.makeText(getApplicationContext(),"Key: "+entry.getKey() + "Value: " + entry.getValue(),Toast.LENGTH_SHORT).show();
            }

i have tried this but also to use the already existing connecting and just use the con .. but it all fails somehow

any help would be really nice

Sonny Hansen
  • 620
  • 1
  • 5
  • 18
  • later in the program im even getting the size of the file with. totalSize = urlConnection.getContentLength(); but i just want the modified date – Sonny Hansen Apr 07 '16 at 09:11

2 Answers2

0

try this may help,

long date = con.getLastModified();

 HttpURLConnection.setFollowRedirects(false);
 HttpURLConnection con = (HttpURLConnection) new URL(fileUrl).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

for reference, http://developer.android.com/reference/java/net/URLConnection.html#getLastModified%28%29

Rakesh
  • 142
  • 2
  • added this piece of code right before my setrequestmethod.. and still my program fails and then instead returns false. and i have checked my returns headers and they do. (tested with postman) – Sonny Hansen Apr 07 '16 at 10:58
-1

i found out that the mistake i made was running it in an async task and then trying to see the result in a Toast.

it was running as soon as i put it into a runOnUiThread

final long date = urlConnection.getLastModified();

        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(),"GGG"+ date,Toast.LENGTH_SHORT).show();
            }
        });
Sonny Hansen
  • 620
  • 1
  • 5
  • 18