Wednesday, 13 April 2016

XML : getContentLength returns -1 when trying to get XML file

I am new to Android programming and would like to get some help on why getContentLength() returns -1 when I try to get the XML data.

The XML url is

  String url = "http://www.systembolaget.se/api/assortment/products/xml";    

    int count;                  try {                  URL url = new URL(aurl[0]);                  URLConnection conexion = url.openConnection();                  conexion.connect();                    int lengthOfFile = conexion.getContentLength();                  Log.d("ANDRO_ASYNC", "Lenght of file: " + lengthOfFile);                    InputStream input = new BufferedInputStream(url.openStream(), 10240);                  File fileDir = getFileFolder(AndroAsync.this);                  File file = new File (fileDir, "systembolaget.xml");                  FileOutputStream outputStream = new FileOutputStream(file);                      byte data[] = new byte[4096];                    long total = 0;                    while ((count = input.read(data)) != -1) {                      total += count;                      publishProgress(""+(int)((total*100)/ lengthOfFile));                      outputStream.write(data, 0, count);                  }                    outputStream.flush();                  outputStream.close();                  input.close();    

All help is much appreciated
Thank you! :)

No comments:

Post a Comment