简体   繁体   English

如何从服务器上的 apk 文件获取上次修改日期

[英]How to get last modified date from apk file on server

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.我试图从我服务器上的 apk 文件中获取上次修改日期,一旦我尝试获取标题,它就会以某种方式失败。

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我已经尝试过这个,但也使用已经存在的连接并只使用 con .. 但它以某种方式失败了

any help would be really nice任何帮助都会非常好

try this may help,试试这可能有帮助,

long date = con.getLastModified();长日期 = 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供参考, http://developer.android.com/reference/java/net/URLConnection.html#getLastModified%28%29

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.我发现我犯的错误是在异步任务中运行它,然后尝试在 Toast 中查看结果。

it was running as soon as i put it into a runOnUiThread我将它放入 runOnUiThread 后它就在运行

final long date = urlConnection.getLastModified();

        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(),"GGG"+ date,Toast.LENGTH_SHORT).show();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM