Recognize the size of the Internet file



  • I've been looking at the English version of this site and found different methods mainly:

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

    But it doesn't work, the app closes when this code's in operation.

    Mistake

    Caused by: android.os.NetworkOnMainThreadException
    

    Cod called onCreatеto check the function

    Is something wrong? Or there's another proven way, for my purpose. I need to compare the file that's already downloaded to what's on the Internet and if the size doesn't match, I'm downloading a new file from the Internet.



  • The method you've brought is based on reading HTTP headlines. Specifically, this heading:

    Content-Length: N
    

    By reading the headings, the data themselves could be discarded, thus reducing the time of receipt of the answer in the Bytes. It's the best way to define "file size."

    Another case is that the server cannot be set up in such a way that the heading will not be included in the server ' s response, and the size of the file will not be determined.

    Your task can be even easier if you use the concept of a hesh soum. If there's a way to do this, use it, because downloading 32 bikes (average hexa size) would be much less problematic than checking the size of the file. By the way, the size may remain the same when some of the reading values are different, which means that the method of verification of the size integrity will fail, while the hex act will affect any change.

    A little offtop:

    Your exception is caused by the fact that Google is tired of hearing that "Android brakes." The system itself is designed so that there are no brakes. No. The problem is, the inexperienced developers ignore many possibilities, which leads to bad applications in the GP that give the impression that Android brakes. You've just ignored one of those, the use of multiplicity. A priori ' s long-term management of network resources is to establish a TCP manual in three packages and then to transmit a data set. It is therefore prohibited in the UI flow.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2