修复无限下载出错的bug

pull/2/head
AriaLyy 8 years ago
parent 32e7c7f235
commit cd55e370cb
  1. 28
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  2. 11
      Aria/src/main/java/com/arialyy/aria/core/task/DownloadUtil.java
  3. 1
      Aria/src/main/java/com/arialyy/aria/core/task/Task.java
  4. 20
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java

@ -199,19 +199,23 @@ public class DownloadSchedulers implements IDownloadSchedulers {
*
* @param entity 失败实体
*/
@Override public void handleFailTask(DownloadEntity entity) {
final Configuration config = Configuration.getInstance();
if (entity.getFailNum() <= config.getReTryNum()) {
Task task = mQueue.getTask(entity);
mQueue.reTryStart(task);
try {
Thread.currentThread().sleep(config.getReTryInterval());
} catch (InterruptedException e) {
e.printStackTrace();
@Override public void handleFailTask(final DownloadEntity entity) {
new Thread(new Runnable() {
@Override public void run() {
final Configuration config = Configuration.getInstance();
if (entity.getFailNum() <= config.getReTryNum()) {
Task task = mQueue.getTask(entity);
mQueue.reTryStart(task);
try {
Thread.sleep(config.getReTryInterval());
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
startNextTask(entity);
}
}
} else {
startNextTask(entity);
}
}).start();
}
/**

@ -229,10 +229,10 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
conn.setConnectTimeout(mConnectTimeOut * 4);
conn.connect();
int len = conn.getContentLength();
if (len < 0) { //网络被劫持时会出现这个问题
failDownload("下载失败,网络被劫持");
return;
}
//if (len < 0) { //网络被劫持时会出现这个问题
// failDownload("下载失败,网络被劫持");
// return;
//}
int code = conn.getResponseCode();
//https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
//206支持断点
@ -240,8 +240,9 @@ final class DownloadUtil implements IDownloadUtil, Runnable {
isSupportBreakpoint = true;
mListener.supportBreakpoint(true);
handleBreakpoint(conn);
} else if (code == HttpURLConnection.HTTP_OK) {
} else if (code == HttpURLConnection.HTTP_OK || len < 0) {
//在conn.setRequestProperty("Range", "bytes=" + 0 + "-");下,200为不支持断点状态
isSupportBreakpoint = false;
mListener.supportBreakpoint(false);
Log.w(TAG, "该下载链接不支持断点下载");
handleBreakpoint(conn);

@ -257,7 +257,6 @@ public class Task {
@Override public void onStart(long startLocation) {
super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
downloadEntity.setFailNum(0);
sendInState2Target(DownloadSchedulers.START);
sendIntent(Aria.ACTION_START, startLocation);
}

@ -54,10 +54,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_RESUME = 0x05;
public static final int DOWNLOAD_COMPLETE = 0x06;
public static final int DOWNLOAD_RUNNING = 0x07;
private static final String DOWNLOAD_URL =
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
//private static final String DOWNLOAD_URL =
// "http://img13.poco.cn/mypoco/myphoto/20120828/15/55689209201208281549023849547194135_001.jpg";
// "http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
private static final String DOWNLOAD_URL =
"http://o.aolcdn.com/hss/storage/midas/3679ed444c51eca8d17a82b7da021b01/204737495/fitbit-charge-2.jpeg";
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@ -124,9 +124,9 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mCancel.setEnabled(false);
setBtState(true);
//String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg";
//Bitmap bm = BitmapFactory.decodeFile(path);
//mImg.setImageBitmap(bm);
String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg";
Bitmap bm = BitmapFactory.decodeFile(path);
mImg.setImageBitmap(bm);
break;
}
@ -203,10 +203,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private void start() {
Aria.whit(this)
.load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
.setDownloadName("test.apk")
//.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.jpg")
//.setDownloadName("test.jpg")
//.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
//.setDownloadName("test.apk")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.jpg")
.setDownloadName("test.jpg")
.start();
}

Loading…
Cancel
Save