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",...