gpt4 book ai didi

java - getContentLength 在尝试获取 XML 文件时返回 -1

转载 作者:行者123 更新时间:2023-11-30 01:24:26 25 4
gpt4 key购买 nike

我是 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();

非常感谢所有的帮助
谢谢你! :)

最佳答案

该服务器未发送内容长度,原因是它正在使用分块传输编码。

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

关于java - getContentLength 在尝试获取 XML 文件时返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36612533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com