Search code examples
javaandroidurlurlconnection

Error when trying to get last modifed time of a file on a server


I'm trying to getLastModifed() time of my text file on a server with URLConnection and getting an error.

Here's the code:

public static String checkLastModified() throws IOException {
    URL url = new URL("http://c24871.shared.hc.ru/Extra.txt");
    URLConnection con = url.openConnection();
    Date lastModified = new Date(con.getLastModified());

    return dff.format(lastModified);
}

If you want more information I'll edit the question.


Solution

  • Finally found the solution. Need just to put all this function in a Thread:

    new Thread(new Runnable() {
            public void run() {
                URL url = null;
                try { url = new URL("http://c24871.shared.hc.ru/Extra.txt");
                } catch (MalformedURLException e) { e.printStackTrace(); }
                connection = null;
                try { connection = url.openConnection();
                } catch (IOException e) { e.printStackTrace(); }
                Log.d(TAG, String.valueOf(connection.getLastModified()));
    
    
            }
        }).start();