http://ws.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=39.998&lng=-82.8841
The gotcha is that you can make only up to 2000 service calls per day, after that you are asked to pay and service does not give you back your so desired zip codes.
And here is how we can do it with groovy and HTTPBuilder. You are going to need the following dependencies, assuming a maven project:
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</ artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy. modules.http-builder</groupId>
<artifactId>http-builder</ artifactId>
<version>0.5.2</version>
</dependency>
And the code:
def http = new HTTPBuilder( 'http://ws.geonames.org' )
http.request( GET, JSON ) {
uri.path = '/findNearbyPostalCodesJSON'
uri.query = [ formatted: true, lat: 39.998, lng: -82.8841 ]
response.success = { resp, json ->
println json
return json.postalCodes[0].postalCode
}
response.failure = { resp ->
log.error("Unexpected error: ${resp.status} : ${resp.statusLine. reasonPhrase}")
return ""
}
Hope this is helpful to someone some day.
Cheers!
No comments:
Post a Comment