Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download Manager support Http code: 302 #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.idea/
.DS_Store
/build
/captures
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public class DownloadRequest {
private String mUri;

private String finalUri;

private File mFolder;

private CharSequence mName;
Expand All @@ -32,6 +34,14 @@ public String getUri() {
return mUri;
}

public String getFinalUri() {
return finalUri;
}

public void setFinalUri(String finalUri) {
this.finalUri = finalUri;
}

public File getFolder() {
return mFolder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.aspsine.multithreaddownload.architecture;

import com.aspsine.multithreaddownload.DownloadException;
import com.aspsine.multithreaddownload.DownloadInfo;

/**
* Created by Aspsine on 2015/10/29.
Expand All @@ -11,7 +10,7 @@ public interface ConnectTask extends Runnable {
interface OnConnectListener {
void onConnecting();

void onConnected(long time, long length, boolean isAcceptRanges);
void onConnected(long time, long length, boolean isAcceptRanges, String finalUri);

void onConnectPaused();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ public void run() {
}

private void executeConnection() throws DownloadException {
executeConnection(mUri, 0);
}

private void executeConnection(String uri, int redirectCount) throws DownloadException {
mStartTime = System.currentTimeMillis();
HttpURLConnection httpConnection = null;
final URL url;
try {
url = new URL(mUri);
url = new URL(uri);
} catch (MalformedURLException e) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "Bad url.", e);
}
Expand All @@ -93,9 +97,20 @@ private void executeConnection() throws DownloadException {
httpConnection.setRequestProperty("Range", "bytes=" + 0 + "-");
final int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
parseResponse(httpConnection, false);
parseResponse(httpConnection, false, uri);
} else if (responseCode == HttpURLConnection.HTTP_PARTIAL) {
parseResponse(httpConnection, true);
parseResponse(httpConnection, true, uri);
} else if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = httpConnection.getHeaderField("Location");
if (location == null) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "response code:"
+ HttpURLConnection.HTTP_MOVED_TEMP + ", but Header Location is null");
} else if (redirectCount > 5) {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "response code:"
+ HttpURLConnection.HTTP_MOVED_TEMP + ",but Temporary Redirect is too many, redirectCount is" + redirectCount);
} else {
executeConnection(location, ++redirectCount);
}
} else {
throw new DownloadException(DownloadStatus.STATUS_FAILED, "UnSupported response code:" + responseCode);
}
Expand All @@ -110,7 +125,7 @@ private void executeConnection() throws DownloadException {
}
}

private void parseResponse(HttpURLConnection httpConnection, boolean isAcceptRanges) throws DownloadException {
private void parseResponse(HttpURLConnection httpConnection, boolean isAcceptRanges, String finalUri) throws DownloadException {

final long length;
String contentLength = httpConnection.getHeaderField("Content-Length");
Expand All @@ -129,7 +144,7 @@ private void parseResponse(HttpURLConnection httpConnection, boolean isAcceptRan
//Successful
mStatus = DownloadStatus.STATUS_CONNECTED;
final long timeDelta = System.currentTimeMillis() - mStartTime;
mOnConnectListener.onConnected(timeDelta, length, isAcceptRanges);
mOnConnectListener.onConnected(timeDelta, length, isAcceptRanges, finalUri);
}

private void checkCanceledOrPaused() throws DownloadException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void onConnecting() {
}

@Override
public void onConnected(long time, long length, boolean isAcceptRanges) {
public void onConnected(long time, long length, boolean isAcceptRanges, String finalUri) {
if (mConnectTask.isCanceled()) {
// despite connection is finished, the entire downloader is canceled
onConnectCanceled();
Expand All @@ -125,6 +125,7 @@ public void onConnected(long time, long length, boolean isAcceptRanges) {

mDownloadInfo.setAcceptRanges(isAcceptRanges);
mDownloadInfo.setLength(length);
mRequest.setFinalUri(finalUri);
download(length, isAcceptRanges);
}
}
Expand Down Expand Up @@ -258,7 +259,7 @@ private List<ThreadInfo> getMultiThreadInfos(long length) {
} else {
end = start + average - 1;
}
ThreadInfo threadInfo = new ThreadInfo(i, mTag, mRequest.getUri(), start, end, 0);
ThreadInfo threadInfo = new ThreadInfo(i, mTag, mRequest.getFinalUri(), start, end, 0);
threadInfos.add(threadInfo);
}
}
Expand All @@ -267,7 +268,7 @@ private List<ThreadInfo> getMultiThreadInfos(long length) {

//TODO
private ThreadInfo getSingleThreadInfo() {
ThreadInfo threadInfo = new ThreadInfo(0, mTag, mRequest.getUri(), 0);
ThreadInfo threadInfo = new ThreadInfo(0, mTag, mRequest.getFinalUri(), 0);
return threadInfo;
}

Expand Down