aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-06-15 23:11:55 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-06-15 23:11:55 +1200
commitc9aaeb052c47634825adb672e3d6316a903f97e9 (patch)
treea5f6038e51515c83bdfb307f73538746be9ca73a
parent9fea9e469c9856f6f6a90fb970474873fe4a9dfb (diff)
downloadtrackermap-server-c9aaeb052c47634825adb672e3d6316a903f97e9.tar.gz
trackermap-server-c9aaeb052c47634825adb672e3d6316a903f97e9.tar.bz2
trackermap-server-c9aaeb052c47634825adb672e3d6316a903f97e9.zip
Handle empty geocoding response
-rw-r--r--src/org/traccar/geocode/GoogleReverseGeocoder.java68
1 files changed, 37 insertions, 31 deletions
diff --git a/src/org/traccar/geocode/GoogleReverseGeocoder.java b/src/org/traccar/geocode/GoogleReverseGeocoder.java
index dae9915d8..efefdb0f6 100644
--- a/src/org/traccar/geocode/GoogleReverseGeocoder.java
+++ b/src/org/traccar/geocode/GoogleReverseGeocoder.java
@@ -18,11 +18,12 @@ package org.traccar.geocode;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
-
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonString;
import org.traccar.helper.Log;
-import javax.json.*;
-
public class GoogleReverseGeocoder implements ReverseGeocoder {
private final String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f";
@@ -35,40 +36,45 @@ public class GoogleReverseGeocoder implements ReverseGeocoder {
URLConnection conn = new URL(String.format(url, latitude, longitude)).openConnection();
JsonObject json = Json.createReader(new InputStreamReader(conn.getInputStream())).readObject();
- JsonObject result = (JsonObject) json.getJsonArray("results").get(0);
- JsonArray components = result.getJsonArray("address_components");
+ JsonArray results = json.getJsonArray("results");
+
+ if (!results.isEmpty()) {
- for (JsonObject component : components.getValuesAs(JsonObject.class)) {
+ JsonObject result = (JsonObject) results.get(0);
+ JsonArray components = result.getJsonArray("address_components");
- String value = component.getString("short_name");
+ for (JsonObject component : components.getValuesAs(JsonObject.class)) {
- for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) {
- if (type.getString().equals("street_number")) {
- address.setHouse(value);
- break;
- } else if (type.getString().equals("route")) {
- address.setStreet(value);
- break;
- } else if (type.getString().equals("locality")) {
- address.setSettlement(value);
- break;
- } else if (type.getString().equals("administrative_area_level_2")) {
- address.setDistrict(value);
- break;
- } else if (type.getString().equals("administrative_area_level_1")) {
- address.setState(value);
- break;
- } else if (type.getString().equals("country")) {
- address.setCountry(value);
- break;
- } else if (type.getString().equals("postal_code")) {
- address.setPostcode(value);
- break;
+ String value = component.getString("short_name");
+
+ for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) {
+ if (type.getString().equals("street_number")) {
+ address.setHouse(value);
+ break;
+ } else if (type.getString().equals("route")) {
+ address.setStreet(value);
+ break;
+ } else if (type.getString().equals("locality")) {
+ address.setSettlement(value);
+ break;
+ } else if (type.getString().equals("administrative_area_level_2")) {
+ address.setDistrict(value);
+ break;
+ } else if (type.getString().equals("administrative_area_level_1")) {
+ address.setState(value);
+ break;
+ } else if (type.getString().equals("country")) {
+ address.setCountry(value);
+ break;
+ } else if (type.getString().equals("postal_code")) {
+ address.setPostcode(value);
+ break;
+ }
}
}
- }
- return format.format(address);
+ return format.format(address);
+ }
} catch(Exception error) {
Log.warning(error);