@Override
public void run() {
try {
downloadTaskObserver.onPendingStarted();
File dir = new File(Environment.getExternalStorageDirectory(), RMConfig.RM_FOLDER);
dir.mkdirs();
String sFileName = dir + "/" + trackInfo.getTrackMetadata().getTrackId() + ".mp3";
File root = new File(sFileName);
if (!root.exists()) {
root.createNewFile();
} else {
return;
}
HttpGet mHttpGet = new HttpGet(trackInfo.getMediaUrl());
HttpResponse response = WSManager.getInstalnce().getRHHttpsClient().execute(mHttpGet);
InputStream zipStream = response.getEntity().getContent();
FileOutputStream fOut = new FileOutputStream(root);
long fileSize = Long.valueOf(response.getFirstHeader("Content-Length").getValue());
long byteCount = 0;
byte[] buffer = new byte[4096];
int bytesRead = -1;
downloadTaskObserver.onDownloadStarted();
while ((bytesRead = zipStream.read(buffer)) != -1 && !cancelDownload) {
fOut.write(buffer, 0, bytesRead);
byteCount += bytesRead;
int value = (int) (byteCount * 100L / fileSize);
downloadTaskObserver.onUpdateProgress(value);
}
if (fOut != null) {
fOut.flush();
fOut.close();
}
if (zipStream != null) {
zipStream.close();
}
if (!cancelDownload) {
downloadTaskObserver.onDownloadFinished();
}
} catch (Exception e) {
e.printStackTrace();
downloadTaskObserver.onDownloadError();
}
}
public interface DownloadTaskObserver {
public void onPendingStarted();
public void onDownloadStarted();
public void onDownloadFinished();
public void onDownloadCanceled();
public void onUpdateProgress(int percent);
public void onDownloadError();
}
public void addToDownloadQueue(GetTrackPlaybackInfo trackInfo, DownloadTaskObserver downloadTaskObserver) {
DownloadTask downloadTask = new DownloadTask(trackInfo, downloadTaskObserver);
mDownloadTasks.add(downloadTask);
// mDownloadTasksPool.execute(downloadTask);
new Thread(downloadTask).start();
}
但是每当我开始下载另一个线程时,它似乎就完成了。我没看到什么可能是错的?提前感谢您的帮助。
I/BrowseActivity(17104): Thread-16 onPendingStarted...
I/BrowseActivity(17104): Thread-16 onDownloadStarted...
E/BrowseActivity(17104): percent 0
E/BrowseActivity(17104): percent 10
E/BrowseActivity(17104): percent 20
Thread-16 onDownloadFinished...
I/BrowseActivity(17104): Thread-17 onPendingStarted...
I/BrowseActivity(17104): Thread-17 onDownloadStarted...
E/BrowseActivity(17104): percent 0
E/BrowseActivity(17104): percent 10
答案 0 :(得分:0)
HttpResponse response = WSManager.getInstalnce().getRHHttpsClient().execute(mHttpGet);
getRHHttpsClient()每次都会为您提供一个新客户端,还是旧的客户端回收?