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
Source (Performance paragraph):http://developer.android.com/reference/java/net/HttpURLConnection.html