use of com.yanzhenjie.nohttp.Headers in project NoHttp by yanzhenjie.
the class Downloader method download.
public void download(int what, DownloadRequest downloadRequest, DownloadListener downloadListener) {
validateParam(downloadRequest, downloadListener);
Connection connection = null;
RandomAccessFile randomAccessFile = null;
String savePathDir = downloadRequest.getFileDir();
String fileName = downloadRequest.getFileName();
try {
if (TextUtils.isEmpty(savePathDir))
savePathDir = NoHttp.getContext().getFilesDir().getAbsolutePath();
validateDevice(savePathDir);
if (// auto named.
TextUtils.isEmpty(fileName))
fileName = Long.toString(System.currentTimeMillis());
File tempFile = new File(savePathDir, fileName + ".nohttp");
// 断点开始处。
long rangeSize = handleRange(tempFile, downloadRequest);
// 连接服务器。
connection = mHttpConnection.getConnection(downloadRequest);
Exception exception = connection.exception();
if (exception != null)
throw exception;
Logger.i("----------Response Start----------");
Headers responseHeaders = connection.responseHeaders();
int responseCode = responseHeaders.getResponseCode();
// getList filename from server.
if (downloadRequest.autoNameByHead()) {
String contentDisposition = responseHeaders.getContentDisposition();
if (!TextUtils.isEmpty(contentDisposition)) {
fileName = HeaderUtil.parseHeadValue(contentDisposition, "filename", null);
if (!TextUtils.isEmpty(fileName)) {
try {
fileName = URLDecoder.decode(fileName, downloadRequest.getParamsEncoding());
} catch (UnsupportedEncodingException e) {
// Do nothing.
}
if (fileName.startsWith("\"") && fileName.endsWith("\"")) {
fileName = fileName.substring(1, fileName.length() - 1);
}
}
}
// From url.
if (TextUtils.isEmpty(fileName)) {
String tempUrl = downloadRequest.url();
String[] slash = tempUrl.split("/");
fileName = slash[slash.length - 1];
int paramIndex = fileName.indexOf("?");
if (paramIndex > 0) {
fileName = fileName.substring(0, paramIndex);
}
}
}
InputStream serverStream = connection.serverStream();
if (responseCode >= 400) {
ServerError error = new ServerError("Download failed, the server response code is " + responseCode + ": " + downloadRequest.url());
error.setErrorBody(IOUtils.toString(serverStream));
throw error;
} else {
long contentLength;
// 文件总大小
if (responseCode == 206) {
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]。
// Sample:Accept-Range:bytes 1024-2047/2048。
String range = responseHeaders.getContentRange();
try {
// 截取'/'之后的总大小。
contentLength = Long.parseLong(range.substring(range.indexOf('/') + 1));
} catch (Throwable e) {
throw new ServerError("ResponseCode is 206, but content-Range error in Server HTTP header " + "information: " + range + ".");
}
} else if (responseCode == 304) {
int httpContentLength = responseHeaders.getContentLength();
downloadListener.onStart(what, true, httpContentLength, responseHeaders, httpContentLength);
downloadListener.onProgress(what, 100, httpContentLength, 0);
Logger.d("-------Download finish-------");
downloadListener.onFinish(what, savePathDir + File.separator + fileName);
return;
} else {
// such as: 200.
// 服务器不支持Range。
rangeSize = 0L;
// 直接下载。
contentLength = responseHeaders.getContentLength();
}
// 验证文件已经存在。
File lastFile = new File(savePathDir, fileName);
if (lastFile.exists()) {
if (downloadRequest.isDeleteOld())
IOUtils.delFileOrFolder(lastFile);
else {
downloadListener.onStart(what, true, lastFile.length(), responseHeaders, lastFile.length());
downloadListener.onProgress(what, 100, lastFile.length(), 0);
Logger.d("-------Download finish-------");
downloadListener.onFinish(what, lastFile.getAbsolutePath());
return;
}
}
if (IOUtils.getDirSize(savePathDir) < contentLength)
throw new StorageSpaceNotEnoughError("The folder is not enough space to save the downloaded file:" + " " + savePathDir + ".");
// 需要重新下载,生成临时文件。
if (responseCode != 206 && !IOUtils.createNewFile(tempFile))
throw new StorageReadWriteError("SD isn't available, please check SD card and permission: " + "WRITE_EXTERNAL_STORAGE, and you must pay attention to Android6.0 RunTime " + "Permissions: https://github.com/yanzhenjie/AndPermission.");
if (downloadRequest.isCanceled()) {
Log.w("NoHttpDownloader", "Download request is canceled.");
downloadListener.onCancel(what);
return;
}
// 通知开始下载了。
Logger.d("-------Download start-------");
downloadListener.onStart(what, rangeSize > 0, rangeSize, responseHeaders, contentLength);
randomAccessFile = new RandomAccessFile(tempFile, "rws");
randomAccessFile.seek(rangeSize);
byte[] buffer = new byte[8096];
int len;
// 旧的进度记录,防止重复通知。
int oldProgress = 0;
// 追加目前已经下载的进度。
long count = rangeSize;
long startTime = System.currentTimeMillis();
long speedCount = 0;
long oldSpeed = 0;
while (((len = serverStream.read(buffer)) != -1)) {
if (downloadRequest.isCanceled()) {
Log.i("NoHttpDownloader", "Download request is canceled.");
downloadListener.onCancel(what);
break;
} else {
randomAccessFile.write(buffer, 0, len);
count += len;
speedCount += len;
long time = System.currentTimeMillis() - startTime;
long speed = speedCount * 1000 / time;
boolean speedChanged = oldSpeed != speed && time >= 300;
Logger.i("speedCount: " + speedCount + "; time: " + time + "; speed: " + speed + "; changed: " + "" + speedChanged);
if (contentLength != 0) {
int progress = (int) (count * 100 / contentLength);
if (progress != oldProgress && speedChanged) {
downloadListener.onProgress(what, progress, count, speed);
speedCount = 0;
oldSpeed = speed;
startTime = System.currentTimeMillis();
} else if (speedChanged) {
downloadListener.onProgress(what, oldProgress, count, speed);
speedCount = 0;
oldSpeed = speed;
startTime = System.currentTimeMillis();
} else if (progress != oldProgress) {
downloadListener.onProgress(what, progress, count, oldSpeed);
}
oldProgress = progress;
} else if (speedChanged) {
downloadListener.onProgress(what, 0, count, speed);
speedCount = 0;
oldSpeed = speed;
startTime = System.currentTimeMillis();
} else {
downloadListener.onProgress(what, 0, count, oldSpeed);
}
}
}
if (!downloadRequest.isCanceled()) {
//noinspection ResultOfMethodCallIgnored
tempFile.renameTo(lastFile);
Logger.d("-------Download finish-------");
downloadListener.onFinish(what, lastFile.getAbsolutePath());
}
}
} catch (MalformedURLException e) {
Logger.e(e);
downloadListener.onDownloadError(what, new URLError(e.getMessage()));
} catch (UnknownHostException e) {
Logger.e(e);
downloadListener.onDownloadError(what, new UnKnownHostError(e.getMessage()));
} catch (SocketTimeoutException e) {
Logger.e(e);
downloadListener.onDownloadError(what, new TimeoutError(e.getMessage()));
} catch (IOException e) {
Exception newException = e;
if (!IOUtils.canWrite(savePathDir))
newException = new StorageReadWriteError("This folder cannot be written to the file: " + savePathDir + ".");
else if (IOUtils.getDirSize(savePathDir) < 1024)
newException = new StorageSpaceNotEnoughError("The folder is not enough space to save the downloaded " + "file: " + savePathDir + ".");
Logger.e(newException);
downloadListener.onDownloadError(what, newException);
} catch (Exception e) {
// NetworkError | ServerError | StorageCantWriteError | StorageSpaceNotEnoughError
if (!NetUtil.isNetworkAvailable())
e = new NetworkError("The network is not available.");
Logger.e(e);
downloadListener.onDownloadError(what, e);
} finally {
Logger.i("----------Response End----------");
IOUtils.closeQuietly(randomAccessFile);
IOUtils.closeQuietly(connection);
}
}
use of com.yanzhenjie.nohttp.Headers in project NoHttp by yanzhenjie.
the class RestResponse method toString.
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Headers headers = getHeaders();
if (headers != null) {
Set<String> keys = headers.keySet();
for (String key : keys) {
List<String> values = headers.getValues(key);
for (String value : values) {
if (key != null) {
builder.append(key).append(": ");
}
builder.append(value).append("\n");
}
}
}
T result = get();
if (result != null)
builder.append(result.toString());
return builder.toString();
}
use of com.yanzhenjie.nohttp.Headers in project NoHttp by yanzhenjie.
the class RedirectActivity method onSucceed.
@Override
public void onSucceed(int what, Response<String> response) {
Headers headers = response.getHeaders();
if (headers.getResponseCode() == 302 || headers.getResponseCode() == 301 || headers.getResponseCode() == 307) {
String message = getString(R.string.request_redirect_location);
message = String.format(Locale.getDefault(), message, headers.getLocation());
showMessageDialog(R.string.request_succeed, message);
} else if (headers.getResponseCode() == 200) {
showMessageDialog(R.string.request_succeed, response.get());
}
}
use of com.yanzhenjie.nohttp.Headers in project NoHttp by yanzhenjie.
the class RestProtocol method setRequestCacheHeader.
/**
* Perform the request before, Handle the cache headers.
*
* @param request the request object.
* @param cacheEntity cached entities.
*/
private void setRequestCacheHeader(IBasicRequest request, CacheEntity cacheEntity) {
if (cacheEntity == null) {
request.headers().remove(Headers.HEAD_KEY_IF_NONE_MATCH);
request.headers().remove(Headers.HEAD_KEY_IF_MODIFIED_SINCE);
} else {
Headers headers = cacheEntity.getResponseHeaders();
String eTag = headers.getETag();
if (eTag != null)
request.headers().set(Headers.HEAD_KEY_IF_NONE_MATCH, eTag);
long lastModified = headers.getLastModified();
if (lastModified > 0)
request.headers().set(Headers.HEAD_KEY_IF_MODIFIED_SINCE, HeaderUtil.formatMillisToGMT(lastModified));
}
}
use of com.yanzhenjie.nohttp.Headers in project NoHttp by yanzhenjie.
the class RedirectActivity method requestDisAllowRedirect.
/**
* 不允许重定向的请求
*/
private void requestDisAllowRedirect() {
Request<String> request = NoHttp.createStringRequest(Constants.URL_NOHTTP_REDIRECT_BAIDU);
request.setRedirectHandler(new RedirectHandler() {
@Override
public IBasicRequest onRedirect(Headers responseHeaders) {
// 不允许重定向时此方法不会被调用。
return null;
}
@Override
public boolean isDisallowedRedirect(Headers responseHeaders) {
// 返回true代表不允许重定向。
return true;
}
});
request(0, request, this, false, true);
}
Aggregations