99import java .net .HttpURLConnection ;
1010import java .net .URL ;
1111
12- import org .json .JSONArray ;
1312import org .json .JSONObject ;
1413
1514/**
@@ -34,7 +33,7 @@ public class What3Words {
3433 /**
3534 * the base URL to the w3w Web-API
3635 */
37- private String baseUrl = "https://api.what3words.com/" ;
36+ private String baseUrl = "https://api.what3words.com/v2/ " ;
3837
3938 /**
4039 * Constructor creating a w3w-object bound to the given API-Key.
@@ -115,18 +114,18 @@ public double[] wordsToPosition(String[] words) throws IOException,
115114 */
116115 public double [] wordsToPosition (String [] words , String language ) throws IOException ,
117116 What3WordsException {
118- String url = String .format ("%sw3w ?key=%s&string =%s.%s.%s&lang=%s" ,this .baseUrl , this .apiKey , words [0 ], words [1 ], words [2 ], language );
117+ String url = String .format ("%sforward ?key=%s&addr =%s.%s.%s&lang=%s" ,this .baseUrl , this .apiKey , words [0 ], words [1 ], words [2 ], language );
119118
120119 String response = this .doHttpGet (url );
121120
122121 try {
123122 // parse the coordinates out of the JSON
124123 JSONObject json = new JSONObject (response );
125- if (json .has ("position " )) {
126- JSONArray jsonCoords = (JSONArray ) json .get ("position " );
124+ if (json .has ("geometry " )) {
125+ JSONObject jsonCoords = (JSONObject ) json .get ("geometry " );
127126 double [] coords = new double [2 ];
128- coords [0 ] = jsonCoords .getDouble ( 0 );
129- coords [1 ] = jsonCoords .getDouble ( 1 );
127+ coords [0 ] = ( Double ) jsonCoords .get ( "lat" );
128+ coords [1 ] = ( Double ) jsonCoords .get ( "lng" );
130129
131130 return coords ;
132131
@@ -201,25 +200,20 @@ public String[] positionToWords(double[] coords)
201200 */
202201 public String [] positionToWords (double [] coords , String language )
203202 throws What3WordsException , IOException {
204- String url = String .format ("%sposition?key=%s&position=%s,%s&lang=%s" ,this .baseUrl , this .apiKey , coords [0 ], coords [1 ], language );
205-
203+ String url = String .format ("%sreverse?key=%s&coords=%s,%s&lang=%s" ,this .baseUrl , this .apiKey , coords [0 ], coords [1 ], language );
206204 String response = this .doHttpGet (url );
207205
208206 try {
209207 // parse the words out of the JSON
210208 JSONObject json = new JSONObject (response );
211209
212210 if (json .has ("words" ) == true ) {
213- JSONArray jsonWords = (JSONArray ) json .get ("words" );
214- String [] words = new String [3 ];
215- words [0 ] = jsonWords .getString (0 );
216- words [1 ] = jsonWords .getString (1 );
217- words [2 ] = jsonWords .getString (2 );
211+ String jsonWords = (String ) json .get ("words" );
212+ String [] words = jsonWords .split ("\\ ." );
218213
219214 return words ;
220215
221- } else if (json .has ("error" )) {
222-
216+ } else if (json .has ("code" )) {
223217 throw new What3WordsException ("Error returned from w3w API: "
224218 + json .getString ("message" ));
225219
@@ -247,12 +241,20 @@ private String doHttpGet(String url) throws IOException {
247241
248242 // ensure we use HTTP GET
249243 con .setRequestMethod ("GET" );
250-
251- BufferedReader in = new BufferedReader (new InputStreamReader (
252- con .getInputStream ()));
253- String inputLine ;
254244 StringBuffer response = new StringBuffer ();
255-
245+ BufferedReader in ;
246+ try {
247+ // HTTP status codes 2xx
248+ in = new BufferedReader (new InputStreamReader (
249+ con .getInputStream ()));
250+
251+ } catch (IOException e ) {
252+ // HTTP status codes other than 2xx
253+ in = new BufferedReader (new InputStreamReader (
254+ con .getErrorStream ()));
255+ }
256+ // Read response body
257+ String inputLine ;
256258 while ((inputLine = in .readLine ()) != null ) {
257259 response .append (inputLine );
258260 }
0 commit comments