- // The actual network request is in a `try` bracket so that `disconnect()` is run in the `finally` section even if an error is encountered in the main block.
- try {
- // Get the content length header, which causes the connection to the server to be made.
- String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
-
- // Define the file size long.
- long fileSize;
-
- // Make sure the content length isn't null.
- if (contentLengthString != null) { // The content length isn't null.
- // Convert the content length to an long.
- fileSize = Long.parseLong(contentLengthString);
- } else { // The content length is null.
- // Set the file size to be `-1`.
- fileSize = -1;
- }
+ // Close the file output stream.
+ fileOutputStream.close();
+ } else { // The URL points to the data location on the internet.
+ // Get the URL from the calling activity.
+ URL url = new URL(urlToSave[0]);
+
+ // Instantiate the proxy helper.
+ ProxyHelper proxyHelper = new ProxyHelper();
+
+ // Get the current proxy.
+ Proxy proxy = proxyHelper.getCurrentProxy(context);
+
+ // Open a connection to the URL. No data is actually sent at this point.
+ HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);