我已经制作了一个可以正常工作的下载方法。它需要 aString url
并返回下载InputStream
的由XMLPullParser
. 现在我想在下载中添加一个进度条。我发现我可以以某种方式使用getContentLength()
onHttpURLConnection
但之后我不知道如何计算进度条。
这是我的代码:
public static InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(25000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setUseCaches(true);
conn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
conn.setRequestProperty("Accept", "application/xml");
conn.connect();
int size = conn.getContentLength();
InputStream stream = conn.getInputStream();
//calculate the progress
return stream;
}
谢谢!