Close url connection at the right time
broke all url loading because you closed the connection before it was actually done being used. This fixes it though.
Fixes
@@ -94,7 +94,21 @@ public class NetworkFetcher {
connection.connect();
if (connection.getErrorStream() != null || connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
+ String error = getErrorFromConnection(connection);
+ return new LottieResult<>(new IllegalArgumentException("Unable to fetch " + url + ". Failed with " + connection.getResponseCode() + "\n" + error));
+ }
+ LottieResult<LottieComposition> result = getResultFromConnection(connection);
+ Logger.debug("Completed fetch from network. Success: " + (result.getValue() != null));
+ return result;
+ } catch (Exception e) {
+ return new LottieResult<>(e);
+ } finally {
+ connection.disconnect();
+ }
+ }
+
+ private String getErrorFromConnection(HttpURLConnection connection) throws IOException {
int responseCode = connection.getResponseCode();
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
StringBuilder error = new StringBuilder();
@@ -107,21 +121,20 @@ public class NetworkFetcher {
} catch (Exception e) {
throw e;
} finally {
+ try {
r.close();
+ } catch (Exception e) {
+ // Do nothing.
}
-
- return new LottieResult<>(new IllegalArgumentException("Unable to fetch " + url + ". Failed with " +
- responseCode + "\n" + error));
}
- } catch (Exception e) {
- return new LottieResult<>(e);
- } finally {
- connection.disconnect();
+ return error.toString();
}
+ @Nullable
+ private LottieResult<LottieComposition> getResultFromConnection(HttpURLConnection connection) throws IOException {
File file;
FileExtension extension;
- LottieResult<LottieComposition> result;
+ LottieResult<LottieComposition> result = null;
switch (connection.getContentType()) {
case "application/zip":
Logger.debug("Handling zip response.");
@@ -141,8 +154,6 @@ public class NetworkFetcher {
if (result.getValue() != null) {
networkCache.renameTempFile(extension);
}
-
- Logger.debug("Completed fetch from network. Success: " + (result.getValue() != null));
return result;
}
}
