The android Getcontentlength () function returned a problem of-1

Source: Internet
Author: User


Let's talk about today's problems. Previously did an upgrade when the progress bar display effect, do not know what is going on today can not be used (the code is not modified), to see the reason is because Getcontentlength () returned 1 caused, why return 1???? Ghost know, look at the server response header file properties, really do not have this property ... So return to-1 .....

Workaround:

Knowing the reason, the solution is very simple, there are two main methods.

One is server-side resolution, which is mainly to add the method.

The second is to call the. Setrequestproperty ("Accept-encoding", "identity") before calling the function, and the function can be obtained by ...


The problem is explained as follows:


By default,HttpURLConnection uses gzip to get, file getcontentlength () This method, each time read completed can be obtained, the current transfer of how much data, and can not use this method to obtain the need to transmit more A few bytes of content, when read () returns 1, the reading is completed, due to the total length of the compression I can not obtain, then the progress bar can not calculate the value.

To get the length, ask the HTTP request not to gzip compress ...

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The android Getcontentlength () function returned a problem of-1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.