-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathUsStreetSingleAddressExample.java
More file actions
64 lines (53 loc) · 3.28 KB
/
UsStreetSingleAddressExample.java
File metadata and controls
64 lines (53 loc) · 3.28 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
package examples;
import com.smartystreets.api.BasicAuthCredentials;
import com.smartystreets.api.exceptions.SmartyException;
import com.smartystreets.api.us_street.*;
import com.smartystreets.api.ClientBuilder;
import java.io.IOException;
import java.util.List;
public class UsStreetSingleAddressExample {
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)
// .withProxy(Proxy.Type.HTTP, "localhost", 8080) // Uncomment this line to try it with a proxy
.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.
lookup.addCustomParameter("source", "example"); // Custom parameters are sent as top-level query params.
client.send(lookup);
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());
} catch (SmartyException | IOException | InterruptedException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}