views:

77

answers:

2

I want to know the length of the file, so i try getContentLength(). It works fine with network connection (edge/3g) but return -1 with WiFi ??? Why ? the wifi is good the file was found, can be download but the return of contentlength is always "-1", i dont understand, please help me. Is there an other way to get the length ?


My code is:

URL url = new URL(file);

URLConnection conexion = url.openConnection();

conexion.connect();

int poids = conexion.getContentLength();
A: 

It may well be the mobile network changing things for you. For example, the mobile network I use shrinks image downloads automatically (and annoyingly). If the network is "transparently" performing the full download before giving you any data, it can fill in the content length for you.

However, you basically shouldn't rely on having the content length... there's nothing to guarantee that it'll be available to you.

Jon Skeet
Well, he's asking why `getContentLength()` returns -1, therefore, it can't be mobile network operator affecting his issue.
The Elite Gentleman
@The Elite Gentleman: Why can't the mobile network operator affect this? I expect that the server involved isn't setting the content length (hence the -1) but that the mobile network operator is furtling with stuff and setting the content length in between. In other words, the -1 is the "normal" situation if you had a straight link directly to the server, and *getting* the content length is the oddity.
Jon Skeet
My scenario will be...if I have a Wifi server @ home, customized by me for Wifi connectivity, and use my Wifi mobile device to connect to my Wifi @ home, how would your scenario "come into play"?
The Elite Gentleman
thank you for these elements ... trying to find a solution because I need to know absolutelythe length to see if I download it or not).I tried another method (HttpURLConnection and forces the HEAD request) but it works in WiFiof sudden it stopped working in 3G/Edge is really too stupid, again, it returns 0 and not -1,which is surprising.Would be stupid to make a different treatment depending on whether you are in wifi or 3g ...
p.b
A: 

The server is probably sending back a HTTP response that is chunked.

The behavior of the getContentLength() method is to return the 'internal' value of the length of the content, that is available to it. When the client receives a HTTP chunked response, the length of the response is not known, and hence the content length value is marked as -1.

The chunked nature of the response can determined by the Transfer-Encoding header value; chunked responses have a value of chunked. HTTP servers need not provide a Content-Length header value if the response is sent via chunked encoding; in fact, servers are encouraged to not send the Content-Length header for a chunked response, for the client is supposed to ignore the Content-Length header.

As for the actual reason on why the server is responding differently in two networks, well it depends on various factors. Usually servers will opt for a more optimal delivery mode, depending on the nature of the client. For some reason, it has detected that it is better off sending chunked responses for one type of a connection. The answer might lie in the HTTP request headers, but not necessarily so.

Vineet Reynolds
thank you for these elements ... trying to find a solution because I need to know absolutelythe length to see if I download it or not).I tried another method (HttpURLConnection and forces the HEAD request) but it works in WiFiof sudden it stopped working in 3G/Edge is really too stupid, again, it returns 0 and not -1,which is surprising.Would be stupid to make a different treatment depending on whether you are in wifi or 3g ...
p.b
Umm... I think you should look at using Apache HTTPClient given your needs. It appears to be present in Android as well http://developer.android.com/reference/org/apache/http/client/HttpClient.html and to be honest, it is more powerful than the URLConnection class. The actual project page is http://hc.apache.org/
Vineet Reynolds
thanks but with apache and HttpClient its same..
p.b
Ah well, I missed the important part. Use Apache HttpClient to issue a HTTP v1.0 HEAD request, as you cannot be sure on whether the response to the HEAD request is also in chunked encoding (worth checking). Chunked tranfer encoding is not supported in HTTP 1.0, so that might help. NB: HTTP 1.1 is more efficient, so it is better to use 1.0 only in this case.
Vineet Reynolds
I'm annoyed, it does not work, I do not know what to do, maybe this is impossible, I'm thinking ...
p.b