我有一个应用程序,用户可能需要下载多达 760 个文件,总计约 350MB。无法压缩这些文件,它们必须作为松散文件下载!
我目前正在使用Android Asynchronous Http Client下载单个文件并AsyncTask
运行整个过程。
DownloadThread
这是一个在后台处理下载数百个文件的对象示例:
public class DownloadThread extends AsyncTask<String,String,String> {
ArrayList<String> list;
AsyncHttpClient client;
String[] allowedContentTypes = new String[] { "audio/mpeg" };
BufferedOutputStream bos;
FileOutputStream fos;
@Override
protected String doInBackground(String... params) {
DownloadTask task;
for (String file : list) {
//the "list" variable has already been populated with hundreds of strings
task = new DownloadTask(file);
task.execute("");
while (!task.isdone)
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
class DownloadTask extends AsyncTask<String, String, String> {
String character, filename;
boolean isdone = false;
public DownloadTask(String file) {
//file = something like "Whale/sadwhale.mp3"
character = file.split("/")[0];
filename = file.split("/")[1];
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(String result) {
if (!result.equals("Error")) {
//Do something on success
}
isdone = true;
}
@Override
protected String doInBackground(String... str) {
client = new AsyncHttpClient();
client.get("http://some-site.com/sounds/" + character + "/"
+ filename, new BinaryHttpResponseHandler(
allowedContentTypes) {
@Override
public void onSuccess(byte[] fileData) {
try {
// Make file/folder and create stream
File folder = new File(Environment
.getExternalStorageDirectory()
+ CharSelect.directory + character);
folder.mkdirs();
File dest = new File(folder, filename);
fos = new FileOutputStream(dest);
bos = new BufferedOutputStream(fos);
// Transfer data to file
bos.write(fileData);
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
return "Success";
}
}
}
DownloadThread
在后台运行,并且还调用数百个它自己的AsyncTasks
. 它一直等到任务完成下载,然后for
为每次下载继续循环。
这行得通,有点。某些下载似乎无法正常完成或根本无法启动。在 760 次下载列表中,平均有 100 次下载正确完成,我必须重新启动该过程以下载另外 100 次下载,直到该下载也失败。我觉得这是由于时间问题,因为这Thread.sleep(10)
条线似乎有点“hackish”。
AsyncTasks
当然,从另一个人那里调用数百个AsyncTask
并不是最有效的方法。如何更改此代码或实施第 3 方解决方案以适应此任务?