From 2b212e1e68dd4c447aaa70b20de16eaa1bf5d0d6 Mon Sep 17 00:00:00 2001 From: Christian Mayer Date: Sat, 15 Oct 2016 12:48:12 +0200 Subject: [PATCH 1/2] Adapt unit test for v2 This adapts an expected error messages, which has changed in w3w REST API v2. --- src/test/java/de/meggsimum/w3w/What3WordsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/de/meggsimum/w3w/What3WordsTest.java b/src/test/java/de/meggsimum/w3w/What3WordsTest.java index c7f75d6..358c698 100644 --- a/src/test/java/de/meggsimum/w3w/What3WordsTest.java +++ b/src/test/java/de/meggsimum/w3w/What3WordsTest.java @@ -98,7 +98,7 @@ public void testChangeLang() throws Exception { @Test public void testWhat3WordsException() throws Exception { expectedException.expect(Exception.class); - expectedException.expectMessage("Error returned from w3w API: Missing or invalid key"); + expectedException.expectMessage("Authentication failed; invalid API key"); What3Words w3w = new What3Words(UUID.randomUUID().toString() + apiKey); double[] coords = {49.422636, 8.320833}; w3w.positionToWords(coords); From de3ce3cd6f8af27afbcb45dcbe3237a8aaa27112 Mon Sep 17 00:00:00 2001 From: Christian Mayer Date: Sat, 15 Oct 2016 12:53:59 +0200 Subject: [PATCH 2/2] Upgrade to w3w REST API v2 This upgrades the w3w-java-wrapper to support the What3Words REST API v2. --- .../java/de/meggsimum/w3w/What3Words.java | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/java/de/meggsimum/w3w/What3Words.java b/src/main/java/de/meggsimum/w3w/What3Words.java index 2de7cc5..20da0c5 100644 --- a/src/main/java/de/meggsimum/w3w/What3Words.java +++ b/src/main/java/de/meggsimum/w3w/What3Words.java @@ -9,7 +9,6 @@ import java.net.HttpURLConnection; import java.net.URL; -import org.json.JSONArray; import org.json.JSONObject; /** @@ -34,7 +33,7 @@ public class What3Words { /** * the base URL to the w3w Web-API */ - private String baseUrl = "https://api.what3words.com/"; + private String baseUrl = "https://api.what3words.com/v2/"; /** * Constructor creating a w3w-object bound to the given API-Key. @@ -115,18 +114,18 @@ public double[] wordsToPosition(String[] words) throws IOException, */ public double[] wordsToPosition(String[] words, String language) throws IOException, What3WordsException { - String url = String.format("%sw3w?key=%s&string=%s.%s.%s&lang=%s" ,this.baseUrl, this.apiKey, words[0], words[1], words[2], language); + String url = String.format("%sforward?key=%s&addr=%s.%s.%s&lang=%s" ,this.baseUrl, this.apiKey, words[0], words[1], words[2], language); String response = this.doHttpGet(url); try { // parse the coordinates out of the JSON JSONObject json = new JSONObject(response); - if (json.has("position")) { - JSONArray jsonCoords = (JSONArray) json.get("position"); + if (json.has("geometry")) { + JSONObject jsonCoords = (JSONObject) json.get("geometry"); double[] coords = new double[2]; - coords[0] = jsonCoords.getDouble(0); - coords[1] = jsonCoords.getDouble(1); + coords[0] = (Double) jsonCoords.get("lat"); + coords[1] = (Double) jsonCoords.get("lng"); return coords; @@ -201,8 +200,7 @@ public String[] positionToWords(double[] coords) */ public String[] positionToWords(double[] coords, String language) throws What3WordsException, IOException { - String url = String.format("%sposition?key=%s&position=%s,%s&lang=%s" ,this.baseUrl, this.apiKey, coords[0], coords[1], language); - + String url = String.format("%sreverse?key=%s&coords=%s,%s&lang=%s" ,this.baseUrl, this.apiKey, coords[0], coords[1], language); String response = this.doHttpGet(url); try { @@ -210,16 +208,12 @@ public String[] positionToWords(double[] coords, String language) JSONObject json = new JSONObject(response); if (json.has("words") == true) { - JSONArray jsonWords = (JSONArray) json.get("words"); - String[] words = new String[3]; - words[0] = jsonWords.getString(0); - words[1] = jsonWords.getString(1); - words[2] = jsonWords.getString(2); + String jsonWords = (String) json.get("words"); + String[] words = jsonWords.split("\\."); return words; - } else if (json.has("error")) { - + } else if (json.has("code")) { throw new What3WordsException("Error returned from w3w API: " + json.getString("message")); @@ -247,12 +241,20 @@ private String doHttpGet(String url) throws IOException { // ensure we use HTTP GET con.setRequestMethod("GET"); - - BufferedReader in = new BufferedReader(new InputStreamReader( - con.getInputStream())); - String inputLine; StringBuffer response = new StringBuffer(); - + BufferedReader in; + try { + // HTTP status codes 2xx + in = new BufferedReader(new InputStreamReader( + con.getInputStream())); + + } catch (IOException e) { + // HTTP status codes other than 2xx + in = new BufferedReader(new InputStreamReader( + con.getErrorStream())); + } + // Read response body + String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); }