All Downloads are FREE. Search and download functionalities are using the official Maven repository.

examples.InternationalAutocompleteExample Maven / Gradle / Ivy

There is a newer version: 3.18.3
Show newest version
package examples;

import com.smartystreets.api.ClientBuilder;
import com.smartystreets.api.SharedCredentials;
import com.smartystreets.api.StaticCredentials;
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;
import java.util.ArrayList;

public class InternationalAutocompleteExample {
    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"));


        //            The appropriate license values to be used for your subscriptions
        //            can be found on the Subscriptions page of the account dashboard.
        //            https://www.smartystreets.com/docs/cloud/licensing
        ArrayList licenses = new ArrayList<>();
        licenses.add("international-autocomplete-v2-cloud");
        Client client = new ClientBuilder(credentials).withLicenses(licenses).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");

        try {
            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());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy