• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

A simple Location and Weather mashup using Gaelyk Framework

June 12, 2012 //  by Mohamed Sanaulla//  Leave a Comment

In our sample Gaelyk application here we stopped at just obtaining the location information. In this post lets update that application to fetch the Weather information as well.

also read:

  • Groovy Tutorials
  • Creating JSON using Groovy
  • Developing Groovy based web application and deploying to Google App Engine

For the weather information we make use of the Weather Underground API which provides a lot of features like geolocation information, current weather conditions, forecast weather conditions among others. You need to sign up to obtain the API KEY.

The URL to request the data is:
[code lang=”html”] http://api.wunderground.com/api/
KEY/FEATURE/[FEATURE…]/[SETTING…]/q/
QUERY.FORMAT
[/code] where,
KEY- API key which you can obtain after sign up
FEATURE- The feature you are interested in: geolocation, conditions, astronomy, radar and so on. One can provide multiple feature data to be retrieved.
FORMAT- The format for the return data- XML or JSON
QUERY- This is the query to be used for retrieving the data- can be city name, pin code, latitude-longitude and others.

For our application we can make use of the following version of the URL:
[code lang=”html”] http://api.wunderground.com/api/
${weatherKey}/conditions/q/
${locationInformation.cityName}.json
[/code] where,
${weatherKey}- will be replaced by the actual weather key
${locationInformation.cityName} will be replaced by actual city name.

Lets create a template- weather.gtpl for showing the Weather information. This template will be added to src/main/webapp/WEB-INF/includes.
[code lang=”html”] <!– weather.gtpl –>
<h3>Weather as of ${request.forecastDate}</h3>
<div class="span3">
<ul>
<li>
<strong>Weather</strong>:
${request.weather}
</li>
<li>
<strong>Temperature</strong>:
${request.temperature}
</li>
<li>
<strong>Wind</strong>:
${request.wind}
</li>
<li>
<strong>Relative Humidity</strong>:
${request.relativeHumidity}
</li>
</ul>
More details
<a href="${request.moreUrl}"
target="_blank">here</a>
</div>
<div class="span2">
<img src="${request.icon}"
alt="${request.iconDesc}" />
</div>
[/code]

And this will be included in the src/main/webapp/WEB-INF/pages/index.gtpl as:
[code lang=”html”] <% include ‘/WEB-INF/includes/header.gtpl’ %>
<div class="row">
<!– Older code here–>
<div class="span6">
<% include ‘/WEB-INF/includes/weather.gtpl’ %>
</div>
</div>
<% include ‘/WEB-INF/includes/footer.gtpl’ %>
[/code]

Now we need to fetch the required information in the Groovlet and set it in the request object. This can be performed in the src/main/webapp/WEB-INF/groovy/index.groovy.
[code lang=”java”] //index.groovy
//Code from the previous sample- removed for clarity
//Fetch the weather information
def weatherKey="YOUR API KEY HERE"
def weatherUrl = """http://api.wunderground.com/api/
${weatherKey}/conditions/q/
${locationInformation.cityName}.json"""
def xmlSlurper = new XmlSlurper()
def weatherInformation = null
if (memcache[locationInformation.cityName] != null){
weatherInformation =
memcache[locationInformation.cityName] }
else{
weatherInformation =
jsonSlurper.parseText(new URL(weatherUrl).text)
//caching the weather for each hour
memcache.put(locationInformation.cityName,
weatherInformation,
Expiration.byDeltaSeconds(3599))
}

def currentObservation =
weatherInformation.current_observation
request.forecastDate =
currentObservation.observation_time_rfc822
request.weather =
currentObservation.weather
request.temperature =
currentObservation.temperature_string
request.wind =
currentObservation.wind_string
request.relativeHumidity =
currentObservation.relative_humidity
request.moreUrl =
currentObservation.ob_url
request.icon =
currentObservation.icon_url
request.iconDesc =
currentObservation.icon

forward ‘/WEB-INF/pages/index.gtpl’

[/code] Once completed your application will look like:

If you want to play around with this code and also the previous sample developed here you can fork the git repo here and clone the repo locally. Once you are done with that just execute gradlew gaeRun on your command line to run this application.

Category: GroovyTag: Gaelyk, Google App Engine

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Previous Post: « Simple introduction to Fork-Join Framework in Java 7
Next Post: Merge Sort implementation using Fork-Join Framework in Java 7 »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact