我是Android编程的新手,希望在尝试获取XML数据时,为getContentLength()
返回-1
的原因提供一些帮助。
XML网址是
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();
非常感谢所有帮助 谢谢! :)
答案 0 :(得分:1)
该服务器没有发送内容长度,原因是它使用的是分块传输编码。
HTTP/1.1 200 OK
Cache-Control: max-age=78517
Content-Type: application/xml; charset=utf-8
ETag: "bb83f8d6-d015-4df5-a8f6-e37248d8a812"
Server: Microsoft-IIS/8.5
X-Cached: True
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Date: Thu, 14 Apr 2016 05:11:22 GMT
X-Cnection: close
Set-Cookie: lbsession=rd10o00000000000000000000ffff91fbfd15o80; path=/
X-Server: 21
Vary: Accept-Encoding
Transfer-Encoding: chunked