diff --git a/.gitignore b/.gitignore
index f6b286c..986c842 100644
--- a/.gitignore
+++ b/.gitignore
@@ -38,3 +38,4 @@ captures/
# Keystore files
*.jks
+.*
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
deleted file mode 100644
index 96cc43e..0000000
--- a/.idea/compiler.xml
+++ /dev/null
@@ -1,22 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml
deleted file mode 100644
index e7bedf3..0000000
--- a/.idea/copyright/profiles_settings.xml
+++ /dev/null
@@ -1,3 +0,0 @@
-
-
-
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
deleted file mode 100644
index c2bae49..0000000
--- a/.idea/encodings.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
deleted file mode 100644
index 32817da..0000000
--- a/.idea/gradle.xml
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/animated_vector_drawable_25_3_0.xml b/.idea/libraries/animated_vector_drawable_25_3_0.xml
deleted file mode 100644
index 2b50f44..0000000
--- a/.idea/libraries/animated_vector_drawable_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/appcompat_v7_25_3_0.xml b/.idea/libraries/appcompat_v7_25_3_0.xml
deleted file mode 100644
index 4b7549f..0000000
--- a/.idea/libraries/appcompat_v7_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/butterknife_8_5_1.xml b/.idea/libraries/butterknife_8_5_1.xml
deleted file mode 100644
index d09d90f..0000000
--- a/.idea/libraries/butterknife_8_5_1.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/butterknife_annotations_8_5_1.xml b/.idea/libraries/butterknife_annotations_8_5_1.xml
deleted file mode 100644
index e2b1710..0000000
--- a/.idea/libraries/butterknife_annotations_8_5_1.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/constraint_layout_1_0_2.xml b/.idea/libraries/constraint_layout_1_0_2.xml
deleted file mode 100644
index c7a10cd..0000000
--- a/.idea/libraries/constraint_layout_1_0_2.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/constraint_layout_solver_1_0_2.xml b/.idea/libraries/constraint_layout_solver_1_0_2.xml
deleted file mode 100644
index 0036a1a..0000000
--- a/.idea/libraries/constraint_layout_solver_1_0_2.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/espresso_core_2_2_2.xml b/.idea/libraries/espresso_core_2_2_2.xml
deleted file mode 100644
index 17ef0d9..0000000
--- a/.idea/libraries/espresso_core_2_2_2.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/espresso_idling_resource_2_2_2.xml b/.idea/libraries/espresso_idling_resource_2_2_2.xml
deleted file mode 100644
index 819ba12..0000000
--- a/.idea/libraries/espresso_idling_resource_2_2_2.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/exposed_instrumentation_api_publish_0_5.xml b/.idea/libraries/exposed_instrumentation_api_publish_0_5.xml
deleted file mode 100644
index f4d57a9..0000000
--- a/.idea/libraries/exposed_instrumentation_api_publish_0_5.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/hamcrest_core_1_3.xml b/.idea/libraries/hamcrest_core_1_3.xml
deleted file mode 100644
index 157e3f3..0000000
--- a/.idea/libraries/hamcrest_core_1_3.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/hamcrest_integration_1_3.xml b/.idea/libraries/hamcrest_integration_1_3.xml
deleted file mode 100644
index 58b2c4b..0000000
--- a/.idea/libraries/hamcrest_integration_1_3.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/hamcrest_library_1_3.xml b/.idea/libraries/hamcrest_library_1_3.xml
deleted file mode 100644
index 676cc63..0000000
--- a/.idea/libraries/hamcrest_library_1_3.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/javawriter_2_1_1.xml b/.idea/libraries/javawriter_2_1_1.xml
deleted file mode 100644
index a66fefb..0000000
--- a/.idea/libraries/javawriter_2_1_1.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/javax_annotation_api_1_2.xml b/.idea/libraries/javax_annotation_api_1_2.xml
deleted file mode 100644
index 811e73f..0000000
--- a/.idea/libraries/javax_annotation_api_1_2.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/javax_inject_1.xml b/.idea/libraries/javax_inject_1.xml
deleted file mode 100644
index 0d1d5fc..0000000
--- a/.idea/libraries/javax_inject_1.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/jsr305_2_0_1.xml b/.idea/libraries/jsr305_2_0_1.xml
deleted file mode 100644
index cdf9878..0000000
--- a/.idea/libraries/jsr305_2_0_1.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/junit_4_12.xml b/.idea/libraries/junit_4_12.xml
deleted file mode 100644
index 305df30..0000000
--- a/.idea/libraries/junit_4_12.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/okhttp_3_6_0.xml b/.idea/libraries/okhttp_3_6_0.xml
deleted file mode 100644
index 472aed6..0000000
--- a/.idea/libraries/okhttp_3_6_0.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/okio_1_11_0.xml b/.idea/libraries/okio_1_11_0.xml
deleted file mode 100644
index 24d5dc9..0000000
--- a/.idea/libraries/okio_1_11_0.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/ormlite_android_4_48.xml b/.idea/libraries/ormlite_android_4_48.xml
deleted file mode 100644
index cd7c6f6..0000000
--- a/.idea/libraries/ormlite_android_4_48.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/ormlite_core_4_48.xml b/.idea/libraries/ormlite_core_4_48.xml
deleted file mode 100644
index c0b727c..0000000
--- a/.idea/libraries/ormlite_core_4_48.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/rules_0_5.xml b/.idea/libraries/rules_0_5.xml
deleted file mode 100644
index 6e02563..0000000
--- a/.idea/libraries/rules_0_5.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/runner_0_5.xml b/.idea/libraries/runner_0_5.xml
deleted file mode 100644
index 1c8a102..0000000
--- a/.idea/libraries/runner_0_5.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_annotations_25_3_0.xml b/.idea/libraries/support_annotations_25_3_0.xml
deleted file mode 100644
index f47ba5d..0000000
--- a/.idea/libraries/support_annotations_25_3_0.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_compat_25_3_0.xml b/.idea/libraries/support_compat_25_3_0.xml
deleted file mode 100644
index cad0467..0000000
--- a/.idea/libraries/support_compat_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_core_ui_25_3_0.xml b/.idea/libraries/support_core_ui_25_3_0.xml
deleted file mode 100644
index a46839e..0000000
--- a/.idea/libraries/support_core_ui_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_core_utils_25_3_0.xml b/.idea/libraries/support_core_utils_25_3_0.xml
deleted file mode 100644
index 9f53566..0000000
--- a/.idea/libraries/support_core_utils_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_fragment_25_3_0.xml b/.idea/libraries/support_fragment_25_3_0.xml
deleted file mode 100644
index be6e08b..0000000
--- a/.idea/libraries/support_fragment_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_media_compat_25_3_0.xml b/.idea/libraries/support_media_compat_25_3_0.xml
deleted file mode 100644
index 7f88908..0000000
--- a/.idea/libraries/support_media_compat_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_v4_25_3_0.xml b/.idea/libraries/support_v4_25_3_0.xml
deleted file mode 100644
index ca119de..0000000
--- a/.idea/libraries/support_v4_25_3_0.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/support_vector_drawable_25_3_0.xml b/.idea/libraries/support_vector_drawable_25_3_0.xml
deleted file mode 100644
index 29be31b..0000000
--- a/.idea/libraries/support_vector_drawable_25_3_0.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index e37300a..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,158 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Abstraction issuesJava
-
-
- Android > Lint > Correctness
-
-
- Android > Lint > Internationalization
-
-
- Android > Lint > Performance
-
-
- Android > Lint > Security
-
-
- Android > Lint > Usability > Icons
-
-
- Code style issuesJava
-
-
- Concurrency annotation issuesJava
-
-
- Declaration redundancyJava
-
-
- Dependency issuesJava
-
-
- Encapsulation issuesJava
-
-
- General
-
-
- Groovy
-
-
- HTML
-
-
- Initialization issuesJava
-
-
- Internationalization issues
-
-
- Internationalization issuesJava
-
-
- Java
-
-
- Java language level migration aidsJava
-
-
- Javadoc issuesJava
-
-
- Memory issuesJava
-
-
- Packaging issuesJava
-
-
- Potentially confusing code constructsGroovy
-
-
- Probable bugsJava
-
-
- RELAX NG
-
-
- Threading issuesJava
-
-
- XML
-
-
-
-
- Android
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index f3899f6..0000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml
deleted file mode 100644
index 7f68460..0000000
--- a/.idea/runConfigurations.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
index 3afce1e..fb600d5 100644
--- a/build.gradle
+++ b/build.gradle
@@ -3,10 +3,11 @@
buildscript {
repositories {
jcenter()
+ google()
}
dependencies {
- classpath 'com.android.tools.build:gradle:2.3.0'
- classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
+ classpath 'com.android.tools.build:gradle:4.0.2'
+ classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
@@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
+ google()
}
}
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index d0cbdfe..1afc415 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
-#Mon Mar 27 10:10:04 CST 2017
+#Thu Dec 24 17:56:54 CST 2020
distributionBase=GRADLE_USER_HOME
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionPath=wrapper/dists
-zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
+zipStoreBase=GRADLE_USER_HOME
diff --git a/okhttp-downloader/build.gradle b/okhttp-downloader/build.gradle
index 220357c..d38ec77 100644
--- a/okhttp-downloader/build.gradle
+++ b/okhttp-downloader/build.gradle
@@ -1,21 +1,22 @@
apply plugin: 'com.android.library'
android {
- compileSdkVersion 25
- buildToolsVersion "25.0.2"
-
+ compileSdkVersion 29
+ buildToolsVersion "30.0.3"
+ lintOptions {
+ abortOnError false
+ }
defaultConfig {
minSdkVersion 15
- targetSdkVersion 25
+ targetSdkVersion 29
versionCode 1
versionName "1.0"
-
}
}
dependencies {
- compile fileTree(include: ['*.jar'], dir: 'libs')
- compile 'com.squareup.okhttp3:okhttp:3.6.0'
- compile 'com.squareup.okio:okio:1.11.0'
+ implementation fileTree(include: ['*.jar'], dir: 'libs')
+ implementation 'com.squareup.okhttp3:okhttp:4.9.0'
+ implementation 'com.squareup.okio:okio:2.8.0'
}
diff --git a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadEngine.java b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadEngine.java
index 3809b0b..d87fa9e 100644
--- a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadEngine.java
+++ b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadEngine.java
@@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
* 下载管理类
@@ -93,12 +94,24 @@ public int getDownloadState(String taskId) {
* @param savePath 文件保存路径
*/
public void download(String taskId, String downloadUrl, String savePath) {
+ download(taskId, downloadUrl, savePath, null);
+ }
+
+ /**
+ * 下载任务的方法
+ *
+ * @param taskId 任务id,需要外界维护和提供
+ * @param downloadUrl 完整下载地址
+ * @param savePath 文件保存路径
+ * @param headers 下载需要的文件头,不需要可以为空
+ */
+ public void download(String taskId, String downloadUrl, String savePath, Map headers) {
L.d("download called! \ntaskId: " + taskId + "\n downloadUrl: " + downloadUrl
+ " \nsavePath: " + savePath);
DownloadInfo downloadInfo = downloadInfoMap.get(taskId);
if (downloadInfo == null) {
//first download.
- downloadInfo = DownloadInfo.create(taskId, downloadUrl, savePath);
+ downloadInfo = DownloadInfo.create(taskId, downloadUrl, savePath, headers);
downloadInfoMap.put(taskId, downloadInfo);
} else {
downloadInfo.downloadUrl = downloadUrl;
diff --git a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadInfo.java b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadInfo.java
index df71f22..3969526 100644
--- a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadInfo.java
+++ b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadInfo.java
@@ -3,54 +3,59 @@
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
+import org.json.JSONObject;
import java.io.Serializable;
+import java.util.Map;
/**
* 下载任务的数据封装
- * @author lxj
*
+ * @author lxj
*/
@DatabaseTable(tableName = "download")
-public class DownloadInfo implements Serializable{
- @DatabaseField(id = true)
- public String taskId;//下载任务的标识,存取的时候用到,
- @DatabaseField
- public long currentLength;//已经下载的大小
- @DatabaseField
- public long size;//总大小
- @DatabaseField
- public int state;
- @DatabaseField
- public String downloadUrl;
- @DatabaseField
- public String path;//下载文件保存的路径
-
- /**
- * 快速初始化DownloadInfo的方法
- * @return
- */
- public static DownloadInfo create(String taskId,String downloadUrl,String savePath){
- DownloadInfo downloadInfo = new DownloadInfo();
- downloadInfo.taskId = taskId;
- downloadInfo.downloadUrl = downloadUrl;
- downloadInfo.currentLength = 0L;
- downloadInfo.state = DownloadEngine.STATE_NONE;//一开始是未下载的状态
-
- downloadInfo.path = savePath;
-
- return downloadInfo;
- }
+public class DownloadInfo implements Serializable {
+ @DatabaseField(id = true)
+ public String taskId;//下载任务的标识,存取的时候用到,
+ @DatabaseField
+ public long currentLength;//已经下载的大小
+ @DatabaseField
+ public long size;//总大小
+ @DatabaseField
+ public int state;
+ @DatabaseField
+ public String downloadUrl;
+ @DatabaseField
+ public String path; //下载文件保存的路径
+ @DatabaseField
+ public String headers;//
+
+ /**
+ * 快速初始化DownloadInfo的方法
+ *
+ * @return
+ */
+ public static DownloadInfo create(String taskId, String downloadUrl, String savePath, Map headers) {
+ DownloadInfo downloadInfo = new DownloadInfo();
+ downloadInfo.taskId = taskId;
+ downloadInfo.downloadUrl = downloadUrl;
+ downloadInfo.currentLength = 0L;
+ downloadInfo.state = DownloadEngine.STATE_NONE;//一开始是未下载的状态
+ downloadInfo.headers = new JSONObject(headers).toString();
+ downloadInfo.path = savePath;
+ return downloadInfo;
+ }
- @Override
- public String toString() {
- return "DownloadInfo{" +
- "id='" + taskId + '\'' +
- ", currentLength=" + currentLength +
- ", size=" + size +
- ", state=" + state +
- ", downloadUrl='" + downloadUrl + '\'' +
- ", path='" + path + '\'' +
- '}';
- }
+ @Override
+ public String toString() {
+ return "DownloadInfo{" +
+ "id='" + taskId + '\'' +
+ ", currentLength=" + currentLength +
+ ", size=" + size +
+ ", state=" + state +
+ ", downloadUrl='" + downloadUrl + '\'' +
+ ", path='" + path + '\'' +
+ ", headers='" + headers + '\'' +
+ '}';
+ }
}
diff --git a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadTask.java b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadTask.java
index 1e0af3f..b1fcf3d 100644
--- a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadTask.java
+++ b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/DownloadTask.java
@@ -4,21 +4,27 @@
* Created by dance on 2017/3/26.
*/
+import okhttp3.Headers;
+import org.json.JSONException;
+import org.json.JSONObject;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Iterator;
/**
* 下载任务类
- * @author lxj
*
+ * @author lxj
*/
-class DownloadTask implements Runnable{
+class DownloadTask implements Runnable {
private DownloadEngine engine;
private DownloadInfo downloadInfo;
HttpStack httpStack = null;
- public DownloadTask(DownloadEngine engine,DownloadInfo downloadInfo) {
+
+ public DownloadTask(DownloadEngine engine, DownloadInfo downloadInfo) {
super();
this.engine = engine;
this.downloadInfo = downloadInfo;
@@ -35,24 +41,39 @@ public void run() {
//consider 2 cases: normal download and break download
File file = new File(downloadInfo.path);
- if(!file.exists() || file.length()!=downloadInfo.currentLength) {
+ if (!file.exists() || file.length() != downloadInfo.currentLength) {
file.delete();//delete invalid file
downloadInfo.currentLength = 0;//reset currentLength
}
+ Headers headers = null;
+ if (downloadInfo.headers != null) {
+ try {
+ JSONObject headerJson = new JSONObject(downloadInfo.headers);
+ Iterator keys = headerJson.keys();
+ Headers.Builder builder = new Headers.Builder();
+ while(keys.hasNext()){
+ String next = keys.next();
+ builder.add(next, (String) headerJson.get(next));
+ }
+ headers = builder.build();
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
//request file from url
- InputStream is = httpStack.download(downloadInfo.downloadUrl);
+ InputStream is = httpStack.download(downloadInfo.downloadUrl, headers);
downloadInfo.size = httpStack.getContentLength();
//6.process io
- if(is!=null){
+ if (is != null) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, true);
- byte[] buffer = new byte[1024*8];
+ byte[] buffer = new byte[1024 * 8];
int len = -1;
- while(downloadInfo.state==DownloadEngine.STATE_DOWNLOADING && downloadInfo.currentLength0 && (len=is.read(buffer))!=-1){
+ while (downloadInfo.state == DownloadEngine.STATE_DOWNLOADING && downloadInfo.currentLength < downloadInfo.size
+ && downloadInfo.size > 0 && (len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
downloadInfo.currentLength += len;
@@ -63,24 +84,24 @@ public void run() {
} catch (Exception e) {
e.printStackTrace();
processErrerState();
- } finally{
+ } finally {
httpStack.close();
try {
- if(fos!=null)fos.close();
+ if (fos != null) fos.close();
} catch (IOException e) {
e.printStackTrace();
}
//when code come to here, there are 2 cases: download finised and pause
- if(file.length()==downloadInfo.size
- && downloadInfo.currentLength==downloadInfo.size
- && downloadInfo.state==DownloadEngine.STATE_DOWNLOADING){
+ if (file.length() == downloadInfo.size
+ && downloadInfo.currentLength == downloadInfo.size
+ && downloadInfo.state == DownloadEngine.STATE_DOWNLOADING) {
downloadInfo.state = DownloadEngine.STATE_FINISH;
}
engine.notifyDownloadUpdate(downloadInfo);
engine.updateDownloadInfo(downloadInfo);
- L.d("download task is over: "+downloadInfo.toString());
+ L.d("download task is over: " + downloadInfo.toString());
}
- }else {
+ } else {
processErrerState();
}
}
diff --git a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/HttpStack.java b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/HttpStack.java
index 72daead..ce59a3d 100644
--- a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/HttpStack.java
+++ b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/HttpStack.java
@@ -1,5 +1,7 @@
package com.lxj.okhttpdownloader.download;
+import okhttp3.Headers;
+
import java.io.InputStream;
/**
@@ -9,7 +11,7 @@
public interface HttpStack {
- InputStream download(String downloadUrl);
+ InputStream download(String downloadUrl, Headers headers);
void close();
diff --git a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/OkHttpStack.java b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/OkHttpStack.java
index b0944db..9adda1f 100644
--- a/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/OkHttpStack.java
+++ b/okhttp-downloader/src/main/java/com/lxj/okhttpdownloader/download/OkHttpStack.java
@@ -3,11 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
-import okhttp3.Call;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
+import okhttp3.*;
/**
* Created by dance on 2017/3/26.
@@ -26,13 +22,15 @@ public OkHttpStack(){
public long getContentLength(){
return contentLength;
}
- @Override
- public InputStream download(String downloadUrl) {
- Request request = new Request.Builder()
+ public InputStream download(String downloadUrl, Headers headers) {
+ Request.Builder builder = new Request.Builder();
+ if (headers!=null){
+ builder.headers(headers);
+ }
+ Request request = builder
.get()
.url(downloadUrl)
.build();
-
Call call = client.newCall(request);
try {
Response response = call.execute();
diff --git a/sample/build.gradle b/sample/build.gradle
index a0a4028..9b798ae 100644
--- a/sample/build.gradle
+++ b/sample/build.gradle
@@ -3,8 +3,8 @@ apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
android {
- compileSdkVersion 25
- buildToolsVersion "25.0.2"
+ compileSdkVersion 29
+ buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.lxj.okhttpdownloaderdemo"
minSdkVersion 15
@@ -23,17 +23,17 @@ android {
}
dependencies {
- compile fileTree(include: ['*.jar'], dir: 'libs')
- androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
+ implementation fileTree(include: ['*.jar'], dir: 'libs')
+ androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
- compile 'com.jakewharton:butterknife:8.5.1'
- compile 'com.android.support:appcompat-v7:25.0.0'
- compile 'com.android.support.constraint:constraint-layout:1.0.2'
- testCompile 'junit:junit:4.12'
+ implementation 'com.jakewharton:butterknife:8.5.1'
+ implementation 'com.android.support:appcompat-v7:28.0.0'
+ implementation 'com.android.support.constraint:constraint-layout:2.0.4'
+ testImplementation 'junit:junit:4.13.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
- compile project(':okhttp-downloader')
- compile 'com.android.support:recyclerview-v7:25.0.0'
- compile 'com.google.code.gson:gson:2.8.0'
- compile 'com.github.bumptech.glide:glide:3.7.0'
+ implementation project(':okhttp-downloader')
+ implementation 'com.android.support:recyclerview-v7:28.0.0'
+ implementation 'com.google.code.gson:gson:2.8.6'
+ implementation 'com.github.bumptech.glide:glide:4.9.0'
}