Skip to content

Commit

Permalink
Cleanup HttpURLConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-osm committed Oct 1, 2024
1 parent 7aa5f7d commit 4cc7fc9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions OsmAnd/src/net/osmand/plus/utils/AndroidNetworkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,9 @@ public static Bitmap downloadImage(OsmandApplication ctx, String url) {

public static String downloadFile(@NonNull String url, @NonNull File fileToSave, boolean gzip, @Nullable IProgress progress) {
String error = null;
HttpURLConnection connection = null;
try {
HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url);
connection = NetworkUtils.getHttpURLConnection(url);
connection.setConnectTimeout(CONNECT_TIMEOUT);
connection.setReadTimeout(READ_TIMEOUT);
if (gzip) {
Expand Down Expand Up @@ -617,6 +618,10 @@ public static String downloadFile(@NonNull String url, @NonNull File fileToSave,
error = CANCELLED_MSG;
}
LOG.warn("Cannot download file: " + url, e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
if (progress != null) {
progress.finishTask();
Expand All @@ -627,11 +632,12 @@ public static String downloadFile(@NonNull String url, @NonNull File fileToSave,
public static long downloadModifiedFile(
@NonNull String url, @NonNull File fileToSave, boolean gzip, long lastTime, @Nullable IProgress progress) {
long result = -1;
HttpURLConnection connection = null;
try {
if (progress != null) {
progress.startTask(url, 0);
}
HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url);
connection = NetworkUtils.getHttpURLConnection(url);
connection.setConnectTimeout(CONNECT_TIMEOUT);
connection.setReadTimeout(READ_TIMEOUT);
if (gzip) {
Expand Down Expand Up @@ -675,6 +681,10 @@ public static long downloadModifiedFile(
LOG.error("UnknownHostException, cannot download file " + url + " " + e.getMessage());
} catch (Exception e) {
LOG.warn("Cannot download file: " + url, e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
if (progress != null) {
progress.finishTask();
Expand Down

0 comments on commit 4cc7fc9

Please sign in to comment.