Pages

Ads 468x60px

Monday, July 23, 2012

Java: Google Weather API

This program will use Google Weather API and parse the XML response of API.


import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class GoogleWeatherAPI {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

        URL googleWeatherXml = new URL("http://www.google.com/ig/api?weather=lahore");
     
        //If you are using proxy only then use this code
        //Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.1.11.11", 8080)); // or whatever your proxy is
        //HttpURLConnection uc = (HttpURLConnection)googleWeatherXml.openConnection(proxy);
     
        URLConnection uc = googleWeatherXml.openConnection();
        uc.connect();
     
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(uc.getInputStream());
        doc.getDocumentElement().normalize();
        System.out.println("Root element " + doc.getDocumentElement().getNodeName());

        // Current Conditions:

        // Get City
        NodeList nodeLst = doc.getElementsByTagName("city");
        Element birthTime = (Element) nodeLst.item(0);
        String value = birthTime.getAttribute("data");
        System.out.println(value);

        // Get Condition
        nodeLst = doc.getElementsByTagName("condition");
        Element curCond = (Element) nodeLst.item(0);
        String curCondStr = curCond.getAttribute("data");
        System.out.println(curCondStr);

        // Get Humidity
        nodeLst = doc.getElementsByTagName("humidity");
        Element curHumidity = (Element) nodeLst.item(0);
        String curHumidityStr = curHumidity.getAttribute("data");
        System.out.println(curHumidityStr);

        // Get Temp F
        nodeLst = doc.getElementsByTagName("temp_f");
        Element curTempF = (Element) nodeLst.item(0);
        String curTempFStr = curTempF.getAttribute("data");
        System.out.println("Degrees F: " + curTempFStr);

        // Get Temp C
        nodeLst = doc.getElementsByTagName("temp_c");
        Element curTempC = (Element) nodeLst.item(0);
        String curTempCStr = curTempC.getAttribute("data");
        System.out.println("Degrees C: " + curTempCStr);

        // end current conditions

        // future forecase:

        NodeList listOfForcasts = doc.getElementsByTagName("forecast_conditions");
        int totalDaysForecasted = listOfForcasts.getLength();
        System.out.println("Total number of days forecasted: " + totalDaysForecasted);

        for (int s = 0; s < listOfForcasts.getLength(); s++) {


            Node fourDayForcastNode = listOfForcasts.item(s);
            if (fourDayForcastNode.getNodeType() == Node.ELEMENT_NODE) {

                Element dayOfWeekElement = (Element) fourDayForcastNode;
                //-------
                NodeList dayOfWeekList = dayOfWeekElement.getElementsByTagName("day_of_week");
                dayOfWeekElement = (Element) dayOfWeekList.item(0);
                String dayOfWeekStr = dayOfWeekElement.getAttribute("data");
                System.out.println("Day of week: " + dayOfWeekStr);

                Element lowTempFElement = (Element) fourDayForcastNode;
                //-------
                NodeList lowTempFList = lowTempFElement.getElementsByTagName("low");
                lowTempFElement = (Element) lowTempFList.item(0);
                String lowTempFStr = lowTempFElement.getAttribute("data");
                System.out.println("Low temp F: " + lowTempFStr);

                Element highTempFElement = (Element) fourDayForcastNode;
                //-------
                NodeList highTempFList = highTempFElement.getElementsByTagName("high");
                highTempFElement = (Element) highTempFList.item(0);
                String highTempFStr = highTempFElement.getAttribute("data");
                System.out.println("High temp F: " + highTempFStr);

                Element conditionElement = (Element) fourDayForcastNode;
                //-------
                NodeList conditionList = conditionElement.getElementsByTagName("condition");
                conditionElement = (Element) conditionList.item(0);
                String conditionStr = conditionElement.getAttribute("data");
                System.out.println("Condition: " + conditionStr);
            }
            // End Future Forecast
        }
    }
}

No comments:

Post a Comment