-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathInternationalAutocompleteExample.java
More file actions
56 lines (47 loc) · 2.55 KB
/
InternationalAutocompleteExample.java
File metadata and controls
56 lines (47 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package examples;
import com.smartystreets.api.BasicAuthCredentials;
import com.smartystreets.api.ClientBuilder;
import com.smartystreets.api.exceptions.SmartyException;
import com.smartystreets.api.international_autocomplete.Client;
import com.smartystreets.api.international_autocomplete.Lookup;
import com.smartystreets.api.international_autocomplete.Candidate;
import java.io.IOException;
public class InternationalAutocompleteExample {
public static void main(String[] args) {
// We recommend storing your authentication credentials in environment variables.
// for client-side requests (browser/mobile), use this code:
// SharedCredentials credentials = new SharedCredentials(System.getenv("SMARTY_AUTH_WEB"), System.getenv("SMARTY_AUTH_REFERER"));
BasicAuthCredentials credentials = new BasicAuthCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN"));
try (Client client = new ClientBuilder(credentials).buildInternationalAutcompleteApiClient()) {
Lookup lookup = new Lookup("Louis");
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/cloud/international-address-autocomplete-api#pro-http-request-url
lookup.setCountry("FRA");
lookup.setLocality("Paris");
lookup.setMaxGroupResults(50);
lookup.setGeolocation(true);
client.send(lookup);
System.out.println("*** Result ***");
System.out.println();
printResult(lookup);
} catch (SmartyException | IOException | InterruptedException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
private static void printResult(Lookup lookup) {
for (Candidate candidate : lookup.getResult()) {
if(candidate.getAddressText() != null) {
System.out.println("Entries: " + candidate.getEntries());
System.out.println("Address Text: " + candidate.getAddressText());
System.out.println("Address ID: " + candidate.getAddressID() + "\n");
} else {
System.out.println("Street: " + candidate.getStreet());
System.out.println("Locality: " + candidate.getLocality());
System.out.println("Administrative Area: " + candidate.getAdministrativeArea());
System.out.println("Postal Code: " + candidate.getPostalCode());
System.out.println("Country: " + candidate.getCountryISO3());
}
}
}
}