Sunday, May 15, 2016

URLConnection.getContentLength() returns -1 Error Solved

When android url connection getContentLength() returning negative value or URLConnection.getContentLength() returns -1 Error solved, This is Worked for me:

urlConnection.setRequestProperty("Accept-Encoding", "identity");
So try this

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Encoding", "identity"); // <--- Add this line
int length = connection.getContentLength(); // i get negetive length