From 55744465b536fec0595ff2489d8e0fdf3008c0ff Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 24 Jun 2015 10:11:43 +1200 Subject: Clean up HTTP connection --- src/org/traccar/geocode/JsonReverseGeocoder.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/org/traccar/geocode/JsonReverseGeocoder.java b/src/org/traccar/geocode/JsonReverseGeocoder.java index cd7dafae5..684bfa0c1 100644 --- a/src/org/traccar/geocode/JsonReverseGeocoder.java +++ b/src/org/traccar/geocode/JsonReverseGeocoder.java @@ -21,6 +21,7 @@ import javax.json.Json; import javax.json.JsonObject; import java.io.InputStreamReader; import java.io.Reader; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; @@ -36,15 +37,19 @@ public abstract class JsonReverseGeocoder implements ReverseGeocoder { public String getAddress(AddressFormat format, double latitude, double longitude) { try { - URLConnection conn = new URL(String.format(url, latitude, longitude)).openConnection(); - Reader reader = new InputStreamReader(conn.getInputStream()); + HttpURLConnection conn = (HttpURLConnection) new URL(String.format(url, latitude, longitude)).openConnection(); try { - Address address = parseAddress(Json.createReader(reader).readObject()); - if (address != null) { - return format.format(address); + Reader reader = new InputStreamReader(conn.getInputStream()); + try { + Address address = parseAddress(Json.createReader(reader).readObject()); + if (address != null) { + return format.format(address); + } + } finally { + reader.close(); } } finally { - reader.close(); + conn.disconnect(); } } catch(Exception error) { Log.warning(error); -- cgit v1.2.3