修改log输出方式

This commit is contained in:
huangzhuanghua
2016-08-11 14:10:41 +08:00
parent 99cfa2080f
commit b211d15b17
10 changed files with 134 additions and 219 deletions

View File

@ -2,9 +2,7 @@ package com.gh.download;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.gh.common.util.Trace;
import com.gh.common.util.Utils;
import org.apache.http.HttpStatus;
@ -17,6 +15,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadThread extends Thread {
private static final int CONNECT_TIME = 5000;
private static final int READ_TIME = 5000;
@ -24,10 +23,10 @@ public class DownloadThread extends Thread {
private DownloadListener listener;
private DownloadStatus status;
private Context context;
// private boolean getNewurlSuccess = false;
public DownloadThread(Context context, DownloadEntry entry,
DownloadListener listener) {
public DownloadThread(Context context,
DownloadEntry entry,
DownloadListener listener) {
this.entry = entry;
this.listener = listener;
this.context = context;
@ -53,56 +52,33 @@ public class DownloadThread extends Thread {
fileOutputStream = new FileOutputStream(entry.getPath());
}
//Httpdns替换地址
// String newUrl = HttpdnsUtils.getUrls(context, entry.getUrl());
// if (newUrl == null) {
// newUrl = entry.getUrl();
// getNewurlSuccess = false;
// Utils.log("HttpDnsService::获取新地址失败,用原地址进行下载,原地址为::"+newUrl);
// } else {
// getNewurlSuccess = true;
// Utils.log("HttpDnsService::获取新地址成功,目标地址为::"+newUrl);
// }
URL url = new URL(entry.getUrl());
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// HttpURLConnection connection = (HttpURLConnection) new URL(newUrl).openConnection();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(CONNECT_TIME);
connection.setReadTimeout(READ_TIME);
connection.setRequestProperty("RANGE",
"bytes=" + targetFile.length() + "-");
//获取新地址成功设置HTTP请求头Host域
// if (getNewurlSuccess && newUrl.endsWith(".apk") ){
// //非.apk,结尾的下载地址不需要加请求头Host域
// connection.setRequestProperty("Host", url.getHost());
// }
Utils.log(DownloadThread.class.getSimpleName(),
"startPosition-->" + targetFile.length());
//设置自动重定向
connection.setInstanceFollowRedirects(true);
Trace.getInstance().debug(DownloadThread.class.getSimpleName(),
"startPosition-->" + targetFile.length());
int code = connection.getResponseCode();
if (code == HttpStatus.SC_MOVED_PERMANENTLY || code == HttpStatus.SC_MOVED_TEMPORARILY) {
if (code == HttpStatus.SC_MOVED_PERMANENTLY
|| code == HttpStatus.SC_MOVED_TEMPORARILY) {
//未自动重定向
String location = connection.getHeaderField("Location");
Utils.log("location = " + location);
// entry.setUrl(location);
url = new URL(location);
connection = (HttpURLConnection) url
.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(CONNECT_TIME);
connection.setReadTimeout(READ_TIME);
connection.setRequestProperty("RANGE",
"bytes=" + targetFile.length() + "-");
// if (getNewurlSuccess && newUrl.endsWith(".apk") ){
// //非.apk,结尾的下载地址不需要加请求头Host域
// connection.setRequestProperty("Host", url.getHost());
// }
Utils.log(DownloadThread.class.getSimpleName(),
"startPosition-->" + targetFile.length());
//设置自动重定向
connection.setInstanceFollowRedirects(true);
}
@ -120,7 +96,7 @@ public class DownloadThread extends Thread {
Utils.log("eTag = " + eTag);
Utils.log("eTag2 = " + eTag2);
listener.onStatusChanged(DownloadStatus.hijack);
Trace.getInstance().debug(DownloadThread.class.getSimpleName(),
Utils.log(DownloadThread.class.getSimpleName(),
"error-->链接被劫持");
interrupt();
} else {
@ -128,11 +104,10 @@ public class DownloadThread extends Thread {
if (entry.getSize() == 0) {
entry.setSize(conentLength);
DownloadDao.getInstance(context).newOrUpdate(entry);
Trace.getInstance().debug(DownloadThread.class.getSimpleName(),
Utils.log(DownloadThread.class.getSimpleName(),
"记录第一次长度");
}
Trace.getInstance().debug(
DownloadThread.class.getSimpleName(),
Utils.log(DownloadThread.class.getSimpleName(),
"progress:" + entry.getProgress() + "/curfilesize:"
+ targetFile.length() + "=====contentLength:"
+ conentLength + "/ totalSize:" + entry.getSize());
@ -141,7 +116,6 @@ public class DownloadThread extends Thread {
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
// listener.onProgressChanged(len);
listener.onProgressChanged(targetFile.length(), len);
if (status == DownloadStatus.pause
|| status == DownloadStatus.cancel) {
@ -154,10 +128,7 @@ public class DownloadThread extends Thread {
listener.onStatusChanged(DownloadStatus.done);
}
bos.flush();
Log.v("bug_test", "flush==>" + targetFile.length() + ",progress==>"
+ entry.getProgress());
Trace.getInstance().debug(
DownloadThread.class.getSimpleName(),
Utils.log(DownloadThread.class.getSimpleName(),
"flush==>" + targetFile.length() + ",progress==>"
+ entry.getProgress());
@ -171,7 +142,7 @@ public class DownloadThread extends Thread {
} else {
listener.onStatusChanged(DownloadStatus.neterror);
}
Trace.getInstance().debug(DownloadThread.class.getSimpleName(),
Utils.log(DownloadThread.class.getSimpleName(),
"exception-->" + e.toString());
}
}