forked from smartystreets/smartystreets-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsStreetSingleAddressExample.java
More file actions
81 lines (68 loc) · 3.7 KB
/
UsStreetSingleAddressExample.java
File metadata and controls
81 lines (68 loc) · 3.7 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package examples;
import com.smartystreets.api.StaticCredentials;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartystreets.api.exceptions.SmartyException;
import com.smartystreets.api.us_street.*;
import com.smartystreets.api.ClientBuilder;
import java.io.IOException;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class UsStreetSingleAddressExample {
public static void main(String[] args) {
// We recommend storing your authentication credentials in environment variables.
// for server-to-server requests, use this code:
StaticCredentials credentials = new StaticCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN"));
// for client-side requests (browser/mobile), use this code:
// SharedCredentials credentials = new SharedCredentials(System.getenv("SMARTY_AUTH_WEB"), System.getenv("SMARTY_AUTH_REFERER"));
Client client = new ClientBuilder(credentials)
// .withProxy(Proxy.Type.HTTP, "localhost", 8080) // Uncomment this line to try it with a proxy
.withCustomBaseUrl(Objects.requireNonNullElse(System.getenv("SMARTY_CUSTOM_BASE_URL"), ""))
.buildUsStreetApiClient();
// Documentation for input fields can be found at:
// https://smartystreets.com/docs/us-street-api#input-fields
Lookup lookup = new Lookup();
lookup.setInputId("24601"); // Optional ID from your system
lookup.setAddressee("John Doe");
lookup.setStreet("1600 Amphitheatre Pkwy");
lookup.setStreet2("closet under the stairs");
lookup.setSecondary("APT 2");
lookup.setUrbanization(""); // Only applies to Puerto Rico addresses
lookup.setCity("Mountain View");
lookup.setState("CA");
lookup.setZipCode("94043");
lookup.setCountySource(CountySource.GEOGRAPHIC);
lookup.setMaxCandidates(3);
lookup.setMatch(MatchType.INVALID); // "invalid" is the most permissive match,
// this will always return at least one result even if the address is invalid.
// Refer to the documentation for additional MatchStrategy options.
try {
client.send(lookup);
}
catch (SmartyException | IOException | InterruptedException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
List<Candidate> results = lookup.getResult();
if (results.isEmpty()) {
System.out.println("No candidates. This means the address is not valid.");
return;
}
Candidate firstCandidate = results.get(0);
System.out.println("There is at least one candidate.\n If the match parameter is set to STRICT, the address is valid.\n Otherwise, check the Analysis output fields to see if the address is valid.\n");
System.out.println("Input ID: " + firstCandidate.getInputId());
System.out.println("ZIP Code: " + firstCandidate.getComponents().getZipCode());
System.out.println("County: " + firstCandidate.getMetadata().getCountyName());
System.out.println("Latitude: " + firstCandidate.getMetadata().getLatitude());
System.out.println("Longitude: " + firstCandidate.getMetadata().getLongitude());
ObjectMapper mapper = new ObjectMapper();
try {
String jsonResponse = mapper.writeValueAsString(results);
System.err.println("<JSONSTART>" + jsonResponse + "<JSONEND>");
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}