aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-06-25 10:43:58 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-06-25 10:43:58 +1200
commit9067f6f540441b839584ff90e5ff858f5433f0c7 (patch)
treea0852f7e2a4c905123d3b2bdf7fa67bf642ffbd5 /src
parent6feff5849f6dca3c94cf03e16e972ae85599e5bc (diff)
downloadtrackermap-server-9067f6f540441b839584ff90e5ff858f5433f0c7.tar.gz
trackermap-server-9067f6f540441b839584ff90e5ff858f5433f0c7.tar.bz2
trackermap-server-9067f6f540441b839584ff90e5ff858f5433f0c7.zip
Use Apache HTTP client
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/geocode/JsonReverseGeocoder.java34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/org/traccar/geocode/JsonReverseGeocoder.java b/src/org/traccar/geocode/JsonReverseGeocoder.java
index 684bfa0c1..3f5fe8bed 100644
--- a/src/org/traccar/geocode/JsonReverseGeocoder.java
+++ b/src/org/traccar/geocode/JsonReverseGeocoder.java
@@ -15,15 +15,17 @@
*/
package org.traccar.geocode;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
import org.traccar.helper.Log;
import javax.json.Json;
import javax.json.JsonObject;
+import javax.json.JsonReader;
import java.io.InputStreamReader;
-import java.io.Reader;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
public abstract class JsonReverseGeocoder implements ReverseGeocoder {
@@ -37,21 +39,29 @@ public abstract class JsonReverseGeocoder implements ReverseGeocoder {
public String getAddress(AddressFormat format, double latitude, double longitude) {
try {
- HttpURLConnection conn = (HttpURLConnection) new URL(String.format(url, latitude, longitude)).openConnection();
+ CloseableHttpClient httpClient = HttpClients.createDefault();
try {
- Reader reader = new InputStreamReader(conn.getInputStream());
+ CloseableHttpResponse response = httpClient.execute(new HttpGet(String.format(url, latitude, longitude)));
try {
- Address address = parseAddress(Json.createReader(reader).readObject());
- if (address != null) {
- return format.format(address);
+ HttpEntity entity = response.getEntity();
+ if (entity != null) {
+ JsonReader reader = Json.createReader(new InputStreamReader(entity.getContent()));
+ try {
+ Address address = parseAddress(reader.readObject());
+ if (address != null) {
+ return format.format(address);
+ }
+ } finally {
+ reader.close();
+ }
}
} finally {
- reader.close();
+ response.close();
}
} finally {
- conn.disconnect();
+ httpClient.close();
}
- } catch(Exception error) {
+ } catch (Exception error) {
Log.warning(error);
}