我正在开发一个 API,该 API 返回一个包含多个 XML 文件的 zip 文件,我必须在提取 zip 文件后单独解析这些文件。
这是链接(将下载 zip 文件):
这是我当前将 zip 文件保存在 sdcard 中的代码:
File root = Environment.getExternalStorageDirectory();
String url= "http://clinicaltrials.gov/ct2/results?term=&recr=&rslt=&type=&cond=&intr=&outc=&lead=&spons=&id=&state1=&cntry1=&state2=&cntry2=&state3=&cntry3=&locn=&gndr=Female&age=0&rcv_s=&rcv_e=&lup_s=&lup_e=xml=true";
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(10000); // timeout 10 secs
conn.connect();
InputStream input = conn.getInputStream();
FileOutputStream fOut = new FileOutputStream(new File(root, "new.zip"));
int byteCount = 0;
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = input.read(buffer)) != -1) {
fOut.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
问题: New.zip 文件正在 sdcard 中创建,但似乎没有下载任何文件,文件大小也为 0kb。 我的代码是否正确,或者我必须使用其他东西来处理 zip 文件。
编辑已解决:
非常抱歉api链接无效...应该是
& 在 studtxml 之前是必需的..
感谢每 1 个快速响应..
最佳答案
您的 .zip 文件 URL 或 .zip 文件大小(0 字节大小) 有问题,因为如果我们下载此 .zip 文件(来自 URL由您提供)从网络浏览器然后也以 0 字节大小下载。
关于Android 从 Api 下载 Zip 并存储在 SD 卡中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8090674/