添加自动恢复一次下载
This commit is contained in:
@ -25,9 +25,7 @@ public class DownloadThread extends Thread {
|
||||
private DownloadStatus status;
|
||||
private Context context;
|
||||
|
||||
public DownloadThread(Context context,
|
||||
DownloadEntity entry,
|
||||
DownloadListener listener) {
|
||||
public DownloadThread(Context context, DownloadEntity entry, DownloadListener listener) {
|
||||
this.entry = entry;
|
||||
this.listener = listener;
|
||||
this.context = context;
|
||||
@ -36,6 +34,16 @@ public class DownloadThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
download();
|
||||
}
|
||||
|
||||
private void download() {
|
||||
Utils.log("url = " + entry.getUrl());
|
||||
if (TextUtils.isEmpty(entry.getUrl())) {
|
||||
listener.onStatusChanged(DownloadStatus.notfound);
|
||||
Utils.log(DownloadThread.class.getSimpleName(), "error-->url is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
BufferedInputStream bis = null;
|
||||
BufferedOutputStream bos = null;
|
||||
@ -48,20 +56,10 @@ public class DownloadThread extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(entry.getUrl())) {
|
||||
listener.onStatusChanged(DownloadStatus.notfound);
|
||||
Utils.log(DownloadThread.class.getSimpleName(), "error-->url is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
Utils.log("url = " + entry.getUrl());
|
||||
|
||||
HttpURLConnection connection = openConnection(new URL(entry.getUrl()), targetFile.length());
|
||||
|
||||
Utils.log(DownloadThread.class.getSimpleName(), "startPosition-->" + targetFile.length());
|
||||
|
||||
int code = connection.getResponseCode();
|
||||
Utils.log("code = " +code);
|
||||
if (code == HttpURLConnection.HTTP_MOVED_PERM
|
||||
|| code == HttpURLConnection.HTTP_MOVED_TEMP) {
|
||||
//未自动重定向
|
||||
@ -71,6 +69,7 @@ public class DownloadThread extends Thread {
|
||||
|
||||
code = connection.getResponseCode();
|
||||
}
|
||||
Utils.log("code = " +code);
|
||||
if (code == HttpURLConnection.HTTP_NOT_FOUND) {
|
||||
// 404 Not Found
|
||||
listener.onStatusChanged(DownloadStatus.notfound);
|
||||
@ -131,15 +130,18 @@ public class DownloadThread extends Thread {
|
||||
listener.onStatusChanged(DownloadStatus.done);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO 默认第一次错误自动恢复一次下载
|
||||
|
||||
String errorMsg = Log.getStackTraceString(e);
|
||||
if (!TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("connection timeout")) {
|
||||
listener.onStatusChanged(DownloadStatus.timeout, errorMsg);
|
||||
if (!entry.isReset()) {
|
||||
entry.setReset(true);
|
||||
download();
|
||||
} else {
|
||||
listener.onStatusChanged(DownloadStatus.neterror, errorMsg);
|
||||
String errorMsg = Log.getStackTraceString(e);
|
||||
if (!TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("connection timeout")) {
|
||||
listener.onStatusChanged(DownloadStatus.timeout, errorMsg);
|
||||
} else {
|
||||
listener.onStatusChanged(DownloadStatus.neterror, errorMsg);
|
||||
}
|
||||
Utils.log(DownloadThread.class.getSimpleName(), "exception-->" + e.toString());
|
||||
}
|
||||
Utils.log(DownloadThread.class.getSimpleName(), "exception-->" + e.toString());
|
||||
} finally {
|
||||
if (bis != null) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user