From c9aaeb052c47634825adb672e3d6316a903f97e9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 15 Jun 2015 23:11:55 +1200 Subject: Handle empty geocoding response --- src/org/traccar/geocode/GoogleReverseGeocoder.java | 68 ++++++++++++---------- 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'src/org/traccar') 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); -- cgit v1.2.3