I searched for the English version of this site and found different methods, basically the following:

public long getFileSize(String string) throws IOException { URL url = new URL(string); URLConnection urlConnection = url.openConnection(); urlConnection.connect(); return urlConnection.getContentLength(); } 

But it does not work, the application closes with the action of this code.

Mistake

 Caused by: android.os.NetworkOnMainThreadException 

The code called in onCreatе to check the function

Maybe something is wrong? Or is there another proven method for my purpose. I need to compare a file that has already been downloaded to the fact that it is on the Internet and if the dimensions do not match, then I download a new file from the Internet.

  • 2
    I see that you are calling this code in a UI thread, and catch a NetworkOnMainThreadException . Call in the same AsyncTask.doInBackground or where you download the file. - zRrr
  • @zRrr no, I called this code in onCreate too and also did not have time for the application to start as it immediately crashed. I wanted to display the size in Toast when I started the application (I checked the function) - Iman
  • @zRrr in the logs I see there is such an error that you wrote, but this code is not in the stream - Iman
  • one
    This code climbs into the network, respectively. the same restrictions apply to it as to any other network code. It will not work on onCreate . - zRrr

1 answer 1

The method that you led is based on reading HTTP headers. Specifically of this heading:

 Content-Length: N 

After reading the headers, you can discard the data itself, thus reducing the time to get the size of the response in bytes. This is the best way to determine "file size".

Another thing is that no server can be configured and in such a way that this header will not be included in the body of the server response, and it will not be possible to determine the file size in this way.

Your task can be solved even easier if you use the concept of hash sums. - If there is such an opportunity, use it - because downloading 32 bytes (average hash size) will be much less problematic than checking the file size. By the way, the size can remain the same if some values ​​of bytes diverge, which means that the method of checking the integrity by size will fail, while the hash function will reflect any changes.

Small offtop:

Your exception is caused by the fact that Google is tired of hearing that "Android slows down." After all, the system itself is designed so that there are no brakes in it. None The problem is that inexperienced developers neglect many features, which leads to the appearance of bad applications in GP, ​​causing the impression that "Android is slowing down." You just neglected one of these - the use of multithreading. The operation of accessing network resources is a priori long: set up a TCP handshake in three packets, and then transfer the array of data. Therefore, it is obviously forbidden to perform it in the UI thread.

  • " Having read the headers, you can discard the data itself " - that is, it does not get, but only HEAD? Or sends GET, and the body of the answer reads on an explicit request? If the second, then this, although it may be quick for the client, but still inhumane in relation to the server :) - PinkTux
  • Specifically, in the case of sabzh — neither one nor the other — such a connection method accepts both the answer and the headers together, so this is a simple GET, from which the length of the answer is then torn out — you have to wait for the entire file to load. - AseN