Check if response code is HTTP_OK before downloading
This commit is contained in:
parent
ff5d382a85
commit
0f76583344
@ -41,54 +41,62 @@ public class HttpDownloader extends Downloader {
|
|||||||
URL url = new URL(status.getFeedFile().getDownload_url());
|
URL url = new URL(status.getFeedFile().getDownload_url());
|
||||||
connection = (HttpURLConnection) url.openConnection();
|
connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||||
if (AppConfig.DEBUG) {
|
int responseCode = connection.getResponseCode();
|
||||||
Log.d(TAG, "Connected to resource");
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
}
|
if (AppConfig.DEBUG) {
|
||||||
if (StorageUtils.externalStorageMounted()) {
|
Log.d(TAG, "Connected to resource");
|
||||||
File destination = new File(status.getFeedFile().getFile_url());
|
}
|
||||||
if (!destination.exists()) {
|
if (StorageUtils.externalStorageMounted()) {
|
||||||
InputStream in = new BufferedInputStream(
|
File destination = new File(status.getFeedFile()
|
||||||
connection.getInputStream());
|
.getFile_url());
|
||||||
out = new BufferedOutputStream(new FileOutputStream(
|
if (!destination.exists()) {
|
||||||
destination));
|
InputStream in = new BufferedInputStream(
|
||||||
byte[] buffer = new byte[BUFFER_SIZE];
|
connection.getInputStream());
|
||||||
int count = 0;
|
out = new BufferedOutputStream(new FileOutputStream(
|
||||||
status.setStatusMsg(R.string.download_running);
|
destination));
|
||||||
if (AppConfig.DEBUG)
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
Log.d(TAG, "Getting size of download");
|
int count = 0;
|
||||||
status.setSize(connection.getContentLength());
|
status.setStatusMsg(R.string.download_running);
|
||||||
if (AppConfig.DEBUG)
|
|
||||||
Log.d(TAG, "Size is " + status.getSize());
|
|
||||||
if (status.getSize() == -1) {
|
|
||||||
status.setSize(DownloadStatus.SIZE_UNKNOWN);
|
|
||||||
}
|
|
||||||
|
|
||||||
long freeSpace = StorageUtils.getFreeSpaceAvailable();
|
|
||||||
if (AppConfig.DEBUG)
|
|
||||||
Log.d(TAG, "Free space is " + freeSpace);
|
|
||||||
if (status.getSize() == DownloadStatus.SIZE_UNKNOWN
|
|
||||||
|| status.getSize() <= freeSpace) {
|
|
||||||
if (AppConfig.DEBUG)
|
if (AppConfig.DEBUG)
|
||||||
Log.d(TAG, "Starting download");
|
Log.d(TAG, "Getting size of download");
|
||||||
while (!cancelled && (count = in.read(buffer)) != -1) {
|
status.setSize(connection.getContentLength());
|
||||||
out.write(buffer, 0, count);
|
if (AppConfig.DEBUG)
|
||||||
status.setSoFar(status.getSoFar() + count);
|
Log.d(TAG, "Size is " + status.getSize());
|
||||||
status.setProgressPercent((int) (((double) status
|
if (status.getSize() == -1) {
|
||||||
.getSoFar() / (double) status.getSize()) * 100));
|
status.setSize(DownloadStatus.SIZE_UNKNOWN);
|
||||||
}
|
}
|
||||||
if (cancelled) {
|
|
||||||
onCancelled();
|
long freeSpace = StorageUtils.getFreeSpaceAvailable();
|
||||||
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG, "Free space is " + freeSpace);
|
||||||
|
if (status.getSize() == DownloadStatus.SIZE_UNKNOWN
|
||||||
|
|| status.getSize() <= freeSpace) {
|
||||||
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG, "Starting download");
|
||||||
|
while (!cancelled
|
||||||
|
&& (count = in.read(buffer)) != -1) {
|
||||||
|
out.write(buffer, 0, count);
|
||||||
|
status.setSoFar(status.getSoFar() + count);
|
||||||
|
status.setProgressPercent((int) (((double) status
|
||||||
|
.getSoFar() / (double) status.getSize()) * 100));
|
||||||
|
}
|
||||||
|
if (cancelled) {
|
||||||
|
onCancelled();
|
||||||
|
} else {
|
||||||
|
onSuccess();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
onSuccess();
|
onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE, null);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE, null);
|
onFail(DownloadError.ERROR_FILE_EXISTS, null);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
onFail(DownloadError.ERROR_FILE_EXISTS, null);
|
onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null);
|
onFail(DownloadError.ERROR_HTTP_DATA_ERROR,
|
||||||
|
String.valueOf(responseCode));
|
||||||
}
|
}
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -102,10 +110,11 @@ public class HttpDownloader extends Downloader {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
onFail(DownloadError.ERROR_IO_ERROR, e.getMessage());
|
onFail(DownloadError.ERROR_IO_ERROR, e.getMessage());
|
||||||
}catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
// might be thrown by connection.getInputStream()
|
// might be thrown by connection.getInputStream()
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
onFail(DownloadError.ERROR_CONNECTION_ERROR, status.getFeedFile().getDownload_url());
|
onFail(DownloadError.ERROR_CONNECTION_ERROR, status.getFeedFile()
|
||||||
|
.getDownload_url());
|
||||||
} finally {
|
} finally {
|
||||||
IOUtils.close(connection);
|
IOUtils.close(connection);
|
||||||
IOUtils.closeQuietly(out);
|
IOUtils.closeQuietly(out);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user