Instantly share code, notes, and snippets.

What would you like to do?
Example code for using Postcoder Web in Java. For more information on Postcoder Web visit
// PostCoder Web Service V3 example
// Allies Computing Ltd 2013
// Demonstrates UK Address lookup rest web service
// Product URL:
// Technical Specs:
// Example output:
// This demo shows how to perform an address lookup and parse the results into Java objects.
// Out of the box it uses a restricted API Key that limits the search to the postcode NR14 7PZ
// To experiment with different search terms, signup for a free evaluation API/Search key at:
// Usage
// 1) Save file as PCWV3Client (to match the class name)
// 3) Compile, e.g. javac
// 4) Run, e.g. java PCWV3Client
// Next steps:
// - To explore different searches, Signup for an API/Search Key and assign to searchKey variable
// Alternatively the code below can be pasted into an online java compiler such as
import java.util.*;
import java.lang.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
public class PCWV3Client {
// address class
private static class Address {
public String organisation;
public String premise;
public String dependentstreet;
public String street;
public String doubledependentlocality;
public String dependentlocality;
public String posttown;
public String county;
public String postcode;
public String summaryline;
@Override public String toString() {
return summaryline;
// root element of service output
// essentially an array of addresses
// jaxb bindings
private static class Addresses {
public Address[] address;
// code entry point
public static void main(String []args)
// API/search key - MUST be supplied to unlock search,
// if this is blank a restricted key is used which limits search to NR147PZ
String searchKey = "";
// processing type - for other options see
String method = "address";
// search string - MUST be supplied, if blank defaults to NR147PZ
String searchTerm = "NR14 7PZ";
System.out.println("PostCoder Web V3 Java Client Snippet\n");
if(searchKey.isEmpty()) {
// no search key supplied - use the restricted evaluation key
searchKey = "PCW45-12345-12345-1234X";
searchTerm = "NR14 7PZ";
"No search-key: using restricted evaluation key - resets search term to NR14 7PZ\n"
+ "To obtain a free evaluation key, signup via "
+ "\n");
// format the url
String uri = String.format("",
// for other uri options see
// call the service
HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection();
connection.setRequestProperty("Accept", "application/xml"); // xml or json permitted, jaxb requires xml
// check for call failure
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { // = HTTP 200 code (OK)
throw new RuntimeException("Failed : HTTP error code : " + connection.getResponseCode());
// convert output to java array using jaxb
Addresses addresses = (Addresses) JAXBContext.newInstance(Addresses.class)
// trivial output - display list of address summaries to console
System.out.println("Results for:" + searchTerm + "\n");
if(addresses.address != null) {
for(int i = 0; i < addresses.address.length; ++i) {
Address address = addresses.address[i];
catch(Exception e) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment