Enzo Tran
Enzo Tran

Reputation: 5850

HttpURLConnection content length returns -1 on android 4.0

I am using this code (inside an AsyncTask) to download files:

URL u = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.connect();

int lenghtOfFile = conn.getContentLength();
Log.d("MyApp", "length of file = " + lenghtOfFile);
...

On Android 2.3, it works well and returns the correct length. Since 4.0 however, it always returns -1. What has been changed between these 2 versions, and how can I fix it?

Upvotes: 1

Views: 1484

Answers (2)

Riddhish Ojha
Riddhish Ojha

Reputation: 17

Commenting this statement, fixed my issue too :

conn.setDoOutput(true);

Upvotes: 0

Enzo Tran
Enzo Tran

Reputation: 5850

Somehow removing

conn.setDoOutput(true); 

fixed it for me.

Upvotes: 2

Related Questions