修复无限下载出错的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 失败实体 * @param entity 失败实体
*/ */
@Override public void handleFailTask(DownloadEntity entity) { @Override public void handleFailTask(final DownloadEntity entity) {
final Configuration config = Configuration.getInstance(); new Thread(new Runnable() {
if (entity.getFailNum() <= config.getReTryNum()) { @Override public void run() {
Task task = mQueue.getTask(entity); final Configuration config = Configuration.getInstance();
mQueue.reTryStart(task); if (entity.getFailNum() <= config.getReTryNum()) {
try { Task task = mQueue.getTask(entity);
Thread.currentThread().sleep(config.getReTryInterval()); mQueue.reTryStart(task);
} catch (InterruptedException e) { try {
e.printStackTrace(); Thread.sleep(config.getReTryInterval());
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
startNextTask(entity);
}
} }
} else { }).start();
startNextTask(entity);
}
} }
/** /**

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

@ -257,7 +257,6 @@ public class Task {
@Override public void onStart(long startLocation) { @Override public void onStart(long startLocation) {
super.onStart(startLocation); super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING); downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
downloadEntity.setFailNum(0);
sendInState2Target(DownloadSchedulers.START); sendInState2Target(DownloadSchedulers.START);
sendIntent(Aria.ACTION_START, startLocation); 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_RESUME = 0x05;
public static final int DOWNLOAD_COMPLETE = 0x06; public static final int DOWNLOAD_COMPLETE = 0x06;
public static final int DOWNLOAD_RUNNING = 0x07; 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 = //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.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart; @Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop; @Bind(R.id.stop) Button mStop;
@ -124,9 +124,9 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mCancel.setEnabled(false); mCancel.setEnabled(false);
setBtState(true); setBtState(true);
//String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg"; String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg";
//Bitmap bm = BitmapFactory.decodeFile(path); Bitmap bm = BitmapFactory.decodeFile(path);
//mImg.setImageBitmap(bm); mImg.setImageBitmap(bm);
break; break;
} }
@ -203,10 +203,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private void start() { private void start() {
Aria.whit(this) Aria.whit(this)
.load(DOWNLOAD_URL) .load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") //.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
.setDownloadName("test.apk") //.setDownloadName("test.apk")
//.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.jpg") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.jpg")
//.setDownloadName("test.jpg") .setDownloadName("test.jpg")
.start(); .start();
} }

Loading…
Cancel
Save