0

I'm downloading a file in my android app and I want to display a progressbar. I need to know the total file size by using this method : here. The problem is that I always get -1 despite the fact that it's set on the server and I can see it from my computer.

This is the code :

URL url = new URL(fileUrl);
URLConnection ucon = url.openConnection();
ucon.connect();
int length = ucon.getContentLength();

What is the problem ?

Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69

1 Answers1

-1

This is not related to Android, most probably it was not sent from the server..please use any sniffing tool to make sure the content length header is there.

Mahdi Hijazi
  • 4,424
  • 3
  • 24
  • 29
  • I did, I used curl and this site : http://web-sniffer.net/. Here is the result : curl -I http://rock1.ceypasbien.fr/classification/export HTTP/1.1 200 OK Server: nginx Date: Sun, 22 Apr 2012 16:20:28 GMT Content-Type: text/xml;charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding Set-Cookie: JSESSIONID=AF002E1DED4D6A167CAC0DB1F4762081; Path=/ **Content-Length: 516941** – Stephane Mathis Apr 22 '12 at 18:50
  • okay..please try to get the length after getting the input stream ucon.getInputStream() – Mahdi Hijazi Apr 22 '12 at 18:59
  • It seems to work on the simulator but not on a real device (galaxy tab 8.9 / android 3.2 or galaxy ace / 2.3.3). – Stephane Mathis Apr 22 '12 at 19:54
  • are you downloading some large content, some operators could decide to omit this header and send the data in chunks through their servers. are you using wifi or 2g/3g networks? – Mahdi Hijazi Apr 22 '12 at 19:58
  • I use both 3G and wifi and I only download this file, so only 500Ko. – Stephane Mathis Apr 22 '12 at 20:29