From 97d7a3e3aed6fdf060ddc381105088f378f65935 Mon Sep 17 00:00:00 2001
From: lyy <511455842@qq.com>
Date: Tue, 4 Oct 2016 23:52:18 +0800
Subject: [PATCH] fix bug
---
.idea/misc.xml | 2 +-
.../downloadutil/entity/DownloadEntity.java | 43 ++++++---------
.../arialyy/downloadutil/orm/DbEntity.java | 16 +++---
.../com/arialyy/downloadutil/orm/DbUtil.java | 4 +-
.../downloadutil/util/DownLoadUtil.java | 43 +++++++--------
.../com/arialyy/downloadutil/util/Util.java | 54 +++++++++++++------
6 files changed, 88 insertions(+), 74 deletions(-)
diff --git a/.idea/misc.xml b/.idea/misc.xml
index fbb68289..5d199810 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -37,7 +37,7 @@
-
+
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/entity/DownloadEntity.java b/downloadutil/src/main/java/com/arialyy/downloadutil/entity/DownloadEntity.java
index d9d88f94..7e0b8e61 100644
--- a/downloadutil/src/main/java/com/arialyy/downloadutil/entity/DownloadEntity.java
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/entity/DownloadEntity.java
@@ -14,51 +14,45 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
/**
* 其它状态
*/
- @Ignore
- public static final int STATE_OTHER = -1;
+ @Ignore public static final int STATE_OTHER = -1;
/**
* 失败状态
*/
- @Ignore
- public static final int STATE_FAIL = 0;
+ @Ignore public static final int STATE_FAIL = 0;
/**
* 完成状态
*/
- @Ignore
- public static final int STATE_COMPLETE = 1;
+ @Ignore public static final int STATE_COMPLETE = 1;
/**
* 停止状态
*/
- @Ignore
- public static final int STATE_STOP = 2;
+ @Ignore public static final int STATE_STOP = 2;
/**
* 未开始状态
*/
- @Ignore
- public static final int STATE_WAIT = 3;
+ @Ignore public static final int STATE_WAIT = 3;
/**
* 下载中
*/
- @Ignore
- public static final int STATE_DOWNLOAD_ING = 4;
+ @Ignore public static final int STATE_DOWNLOAD_ING = 4;
/**
* 取消下载
*/
- @Ignore
- public static final int STATE_CANCEL = 5;
-
- private String downloadUrl; //下载路径
- private String downloadPath; //保存路径
- private String fileName; //文件名
- private String str; //其它字段
- private long completeTime; //完成时间
+ @Ignore public static final int STATE_CANCEL = 5;
+
+ private String downloadUrl = ""; //下载路径
+ private String downloadPath = ""; //保存路径
+ private String fileName = ""; //文件名
+ private String str = ""; //其它字段
+ private long completeTime; //完成时间
private long fileSize = 1;
private int state = STATE_WAIT;
private boolean isDownloadComplete = false; //是否下载完成
private long currentProgress = 0; //当前下载进度
private int failNum = 0;
- public DownloadEntity(){}
+ public DownloadEntity() {
+ }
public String getStr() {
return str;
@@ -154,9 +148,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
dest.writeLong(this.completeTime);
dest.writeLong(this.fileSize);
dest.writeInt(this.state);
- dest.writeByte(this.isDownloadComplete ?
- (byte) 1 :
- (byte) 0);
+ dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
dest.writeLong(this.currentProgress);
dest.writeInt(this.failNum);
}
@@ -172,8 +164,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
this.failNum = in.readInt();
}
- @Ignore
- public static final Creator CREATOR = new Creator() {
+ @Ignore public static final Creator CREATOR = new Creator() {
@Override public DownloadEntity createFromParcel(Parcel source) {
return new DownloadEntity(source);
}
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java
index 21793b8c..73dabb00 100644
--- a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java
@@ -2,6 +2,8 @@ package com.arialyy.downloadutil.orm;
import android.support.annotation.NonNull;
+import com.arialyy.downloadutil.util.Util;
+
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@@ -57,8 +59,8 @@ public class DbEntity {
/**
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
*/
- public void save() {
- if (thisIsExist()) {
+ public synchronized void save() {
+ if (mUtil.tableExists(this) && thisIsExist()) {
update();
} else {
insert();
@@ -70,7 +72,7 @@ public class DbEntity {
*/
private boolean thisIsExist() {
try {
- Field[] fields = getClass().getFields();
+ Field[] fields = Util.getFields(getClass());
List where = new ArrayList<>();
List values = new ArrayList<>();
for (Field field : fields) {
@@ -80,10 +82,10 @@ public class DbEntity {
continue;
}
where.add(field.getName());
- values.add((String) field.get(getClass()));
+ values.add(field.get(this) + "");
}
- return findData(getClass(), (String[]) where.toArray(),
- (String[]) values.toArray()) != null;
+ return findData(getClass(), where.toArray(new String[where.size()]),
+ values.toArray(new String[values.size()])) != null;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
@@ -124,6 +126,6 @@ public class DbEntity {
public T findData(Class clazz, @NonNull String[] wheres,
@NonNull String[] values) {
List datas = mUtil.findData(clazz, this, wheres, values);
- return datas.size() > 0 ? datas.get(0) : null;
+ return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
}
}
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java
index 800210ed..37780f32 100644
--- a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java
@@ -149,7 +149,7 @@ public class DbUtil {
int i = 0;
for (Object where : wheres) {
sb.append(where).append("=").append("'").append(values[i]).append("'");
- sb.append(i >= wheres.length - 1 ? "" : ", ");
+ sb.append(i >= wheres.length - 1 ? "" : " AND ");
i++;
}
print(FIND_DATA, sb.toString());
@@ -208,7 +208,7 @@ public class DbUtil {
/**
* 查找某张表是否存在
*/
- public boolean tableExists(DbEntity dbEntity) {
+ public synchronized boolean tableExists(DbEntity dbEntity) {
Cursor cursor = null;
try {
StringBuilder sb = new StringBuilder();
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java
index 5e81b9ea..fc3c3491 100644
--- a/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java
@@ -25,16 +25,16 @@ public class DownLoadUtil {
/**
* 线程数
*/
- private static final int THREAD_NUM = 3;
+ private static final int THREAD_NUM = 3;
+ private static final int TIME_OUT = 5000; //超时时间
/**
* 已经完成下载任务的线程数量
*/
- private int mCompleteThreadNum = 0;
+ private int mCompleteThreadNum = 0;
+ private boolean isDownloading = false;
+ private boolean isStop = false;
+ private boolean isCancel = false;
private long mCurrentLocation;
- private boolean isDownloading = false;
- private boolean isStop = false;
- private boolean isCancel = false;
- private static final int TIME_OUT = 5000; //超时时间
boolean isNewTask = true;
private int mCancelNum = 0;
private int mStopNum = 0;
@@ -114,9 +114,9 @@ public class DownLoadUtil {
conn.setRequestProperty("Charset", "UTF-8");
conn.setConnectTimeout(TIME_OUT * 4);
conn.setRequestProperty("User-Agent",
- "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Accept",
- "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
+ "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.connect();
int len = conn.getContentLength();
if (len < 0) { //网络被劫持时会出现这个问题
@@ -197,7 +197,8 @@ public class DownLoadUtil {
endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
}
DownloadEntity entity = new DownloadEntity(context, fileLength,
- downloadUrl, dFile, i, startL, endL);
+ downloadUrl, dFile, i,
+ startL, endL);
DownLoadTask task = new DownLoadTask(entity);
tasks.put(i, new Thread(task));
}
@@ -230,7 +231,6 @@ public class DownLoadUtil {
isDownloading = false;
stopDownload();
mListener.onFail();
- System.gc();
}
/**
@@ -244,26 +244,26 @@ public class DownLoadUtil {
public DownLoadTask(DownloadEntity downloadInfo) {
this.dEntity = downloadInfo;
configFPath = dEntity.context.getFilesDir()
- .getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties";
+ .getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties";
}
@Override public void run() {
long currentLocation = 0;
try {
Log.d(TAG,
- "线程_" + dEntity.threadId + "_正在下载【" + "开始位置 : " + dEntity.startLocation + ",结束位置:" + dEntity.endLocation + "】");
+ "线程_" + dEntity.threadId + "_正在下载【" + "开始位置 : " + dEntity.startLocation + ",结束位置:" + dEntity.endLocation + "】");
URL url = new URL(dEntity.downloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//在头里面请求下载开始位置和结束位置
conn.setRequestProperty("Range",
- "bytes=" + dEntity.startLocation + "-" + dEntity.endLocation);
+ "bytes=" + dEntity.startLocation + "-" + dEntity.endLocation);
conn.setRequestMethod("GET");
conn.setRequestProperty("Charset", "UTF-8");
conn.setConnectTimeout(TIME_OUT * 4);
conn.setRequestProperty("User-Agent",
- "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Accept",
- "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
+ "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数
InputStream is = conn.getInputStream();
//创建可设置位置的文件
@@ -306,7 +306,6 @@ public class DownLoadUtil {
Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++");
isDownloading = false;
mListener.onCancel();
- System.gc();
}
}
return;
@@ -317,14 +316,13 @@ public class DownLoadUtil {
mStopNum++;
String location = String.valueOf(currentLocation);
Log.i(TAG,
- "thread_" + dEntity.threadId + "_stop, stop location ==> " + currentLocation);
+ "thread_" + dEntity.threadId + "_stop, stop location ==> " + currentLocation);
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId,
- location);
+ location);
if (mStopNum == THREAD_NUM) {
Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++");
isDownloading = false;
mListener.onStop(mCurrentLocation);
- System.gc();
}
}
return;
@@ -340,7 +338,6 @@ public class DownLoadUtil {
}
isDownloading = false;
mListener.onComplete();
- System.gc();
}
} catch (MalformedURLException e) {
e.printStackTrace();
@@ -349,7 +346,7 @@ public class DownLoadUtil {
try {
String location = String.valueOf(currentLocation);
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId,
- location);
+ location);
failDownload("下载链接异常");
} catch (IOException e1) {
e1.printStackTrace();
@@ -360,7 +357,7 @@ public class DownLoadUtil {
try {
String location = String.valueOf(currentLocation);
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId,
- location);
+ location);
failDownload(
"下载失败【" + dEntity.downloadUrl + "】" + Util.getPrintException(e));
} catch (IOException e1) {
@@ -372,7 +369,7 @@ public class DownLoadUtil {
try {
String location = String.valueOf(currentLocation);
writeConfig(dEntity.tempFile.getName() + "_record_" + dEntity.threadId,
- location);
+ location);
failDownload("获取流失败" + Util.getPrintException(e));
} catch (IOException e1) {
e1.printStackTrace();
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java
index 9ac0cd79..197cb810 100644
--- a/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java
@@ -18,6 +18,45 @@ import java.util.Properties;
public class Util {
private static final String TAG = "util";
+ /**
+ * 获取类里面的所在字段
+ */
+ public static Field[] getFields(Class clazz) {
+ Field[] fields = null;
+ fields = clazz.getDeclaredFields();
+ if (fields == null || fields.length == 0) {
+ Class superClazz = clazz.getSuperclass();
+ if (superClazz != null) {
+ fields = getFields(superClazz);
+ }
+ }
+ return fields;
+ }
+
+ /**
+ * 获取类里面的指定对象,如果该类没有则从父类查询
+ */
+ public static Field getField(Class clazz, String name) {
+ Field field = null;
+ try {
+ field = clazz.getDeclaredField(name);
+ } catch (NoSuchFieldException e) {
+ try {
+ field = clazz.getField(name);
+ } catch (NoSuchFieldException e1) {
+ if (clazz.getSuperclass() == null) {
+ return field;
+ } else {
+ field = getField(clazz.getSuperclass(), name);
+ }
+ }
+ }
+ if (field != null) {
+ field.setAccessible(true);
+ }
+ return field;
+ }
+
/**
* 将缓存的key转换为hash码
*
@@ -53,21 +92,6 @@ public class Util {
return stringBuilder.toString();
}
- /**
- * 获取类里面的所在字段
- */
- public static Field[] getFields(Class clazz) {
- Field[] fields = null;
- fields = clazz.getDeclaredFields();
- if (fields == null || fields.length == 0) {
- Class superClazz = clazz.getSuperclass();
- if (superClazz != null) {
- fields = getFields(superClazz);
- }
- }
- return fields;
- }
-
/**
* 获取对象名
*