Check if response code is HTTP_OK before downloading
This commit is contained in:
parent
ff5d382a85
commit
0f76583344
@ -41,11 +41,14 @@ public class HttpDownloader extends Downloader {
|
||||
URL url = new URL(status.getFeedFile().getDownload_url());
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
if (AppConfig.DEBUG) {
|
||||
Log.d(TAG, "Connected to resource");
|
||||
}
|
||||
if (StorageUtils.externalStorageMounted()) {
|
||||
File destination = new File(status.getFeedFile().getFile_url());
|
||||
File destination = new File(status.getFeedFile()
|
||||
.getFile_url());
|
||||
if (!destination.exists()) {
|
||||
InputStream in = new BufferedInputStream(
|
||||
connection.getInputStream());
|
||||
@ -70,7 +73,8 @@ public class HttpDownloader extends Downloader {
|
||||
|| status.getSize() <= freeSpace) {
|
||||
if (AppConfig.DEBUG)
|
||||
Log.d(TAG, "Starting download");
|
||||
while (!cancelled && (count = in.read(buffer)) != -1) {
|
||||
while (!cancelled
|
||||
&& (count = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, count);
|
||||
status.setSoFar(status.getSoFar() + count);
|
||||
status.setProgressPercent((int) (((double) status
|
||||
@ -90,6 +94,10 @@ public class HttpDownloader extends Downloader {
|
||||
} else {
|
||||
onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null);
|
||||
}
|
||||
} else {
|
||||
onFail(DownloadError.ERROR_HTTP_DATA_ERROR,
|
||||
String.valueOf(responseCode));
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
onFail(DownloadError.ERROR_MALFORMED_URL, e.getMessage());
|
||||
@ -105,7 +113,8 @@ public class HttpDownloader extends Downloader {
|
||||
} catch (NullPointerException e) {
|
||||
// might be thrown by connection.getInputStream()
|
||||
e.printStackTrace();
|
||||
onFail(DownloadError.ERROR_CONNECTION_ERROR, status.getFeedFile().getDownload_url());
|
||||
onFail(DownloadError.ERROR_CONNECTION_ERROR, status.getFeedFile()
|
||||
.getDownload_url());
|
||||
} finally {
|
||||
IOUtils.close(connection);
|
||||
IOUtils.closeQuietly(out);
|
||||
|
Loading…
x
Reference in New Issue
Block a user