Android : Android Http ContentLength always -1

on Friday, September 5, 2014


I need a method that determines the size of a file on the Internet. This is my method:



private int getSize(String url){
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
//connection.setConnectTimeout(TIMEOUT);
//connection.setReadTimeout(TIMEOUT);
//connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
int size = connection.getContentLength();

if(responseCode != 200){
Log.e(LOG_TAG, "Fehlercode: " + responseCode + " beim Ermitteln der Dateigroesse von " + url);
return -1;
}

return size;
} catch (IOException e) {
Log.e(LOG_TAG, "Fehler beim Ermitteln der Dateigroesse von " + url, e);
return -1;
}
}


With Java (desktop) the method works - on Android it doesn't work. What's wrong with that?


Thanks for the help!


0 comments:

Post a Comment