Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/main/java/de/meggsimum/w3w/What3Words.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONObject;

/**
* A Java wrapper for the What3Words Web-API.
*
* @see http://developer.what3words.com/api/
* @see <a href="http://developer.what3words.com/api/">http://developer.what3words.com/api/</a>
*
* @author Christian Mayer, meggsimum
*/
Expand Down Expand Up @@ -83,14 +84,14 @@ 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("%sw3w?key=%s&string=%s.%s.%s&lang=%s" ,this.baseUrl, URLEncoder.encode(this.apiKey, "UTF-8"), URLEncoder.encode(words[0], "UTF-8"), URLEncoder.encode(words[1], "UTF-8"), URLEncoder.encode(words[2], "UTF-8"), language);

String response = this.doHttpGet(url);

try {
// parse the coordinates out of the JSON
JSONObject json = new JSONObject(response);
if (json.has("position") == true) {
if (json.has("position")) {
JSONArray jsonCoords = (JSONArray) json.get("position");
double[] coords = new double[2];
coords[0] = jsonCoords.getDouble(0);
Expand Down Expand Up @@ -141,15 +142,15 @@ 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("%sposition?key=%s&position=%s,%s&lang=%s" ,this.baseUrl, URLEncoder.encode(this.apiKey, "UTF-8"), coords[0], coords[1], language);

String response = this.doHttpGet(url);

try {
// parse the words out of the JSON
JSONObject json = new JSONObject(response);

if (json.has("words") == true) {
if (json.has("words")) {
JSONArray jsonWords = (JSONArray) json.get("words");
String[] words = new String[3];
words[0] = jsonWords.getString(0);
Expand Down Expand Up @@ -191,7 +192,7 @@ private String doHttpGet(String url) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/de/meggsimum/w3w/What3WordsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,14 @@ public void testWhat3WordsException() {
assertTrue(thrown);
}

/**
* Test translating non-ascii words to position.
* Correct URL encoding is required for this to pass.
*/
public void testNonAsciiCharacters() throws Exception {
What3Words w3w = new What3Words(API_KEY, "de");
String[] words = {"winkel", "artenschutz","fängen"};
w3w.wordsToPosition(words);
}

}