Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

How to Make Your Own Android Weather Application Using Google Weather API

4.00/5 (9 votes)
20 Mar 2014CPOL 40.4K   1.1K  
This tip shows you how to make your own Android Weather Application using XML Parsing.

Introduction

If you want to make your own simple Android Weather application, you are at the right place. It is more simple if you use XML Parsing of Google Weather API. So you need to understand the XML Parsing Process, not more.

Background

We need to use the Simple Access XML to parse the XML Document. So, you have to just know the location and the place of the result in the code of XML Document and parse it easily. For example, we want to know the weather related to Sfax Tunisia in this picture shown below:

Image 1

Using the Code

At the beginning, we need to specify the City or the State for which we would like to know the weather description.

Java
String c = city.getText().toString();
        String s = state.getText().toString();
        StringBuilder URL = new StringBuilder(BaseURL); 
        URL.append(c+","+s);
        String fullUrl= URL.toString();
        try
        {
            URL website= new URL(fullUrl);
            //getting xmlReader to parse data
            SAXParserFactory spf= SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader() ;
            HandlingXmlStuff doingWork = new HandlingXmlStuff();
            xr.setContentHandler(doingWork);
            xr.parse(new InputSource(website.openStream()));
            String information = doingWork.getInformation();
            tv.setText(information);
        }
        catch(Exception e)
        {
            tv.setText("error");
        } 

After that, we need to start parsing the XML Document.

Java
public class HandlingXmlStuff extends DefaultHandler {
XMLDataCollected info = new XMLDataCollected();
public String getInformation()
{
    return info.dataToString();
}

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if (localName.equals("city"))
        {
            String city=attributes.getValue("data");
            info.setCity(city);
        }else if (localName.equals("temp_f")){
            String t = attributes.getValue("data");
            int temp = Integer.parseInt(t);
            info.setTemp(temp);
        }      
    }
} 

We need to specify our data model and the functions used.

Java
public class XMLDataCollected {
    int temp= 0;
    String city=null ; 
    public void setCity(String c)
    {
        city= c ; 
    }
    public void setTemp(int t )
    {
        temp = t ;
    }
    public String dataToString()
    {
        return "In"+city+" the current Temp in F is "+ temp+" degrees";
    }
} 

Points of Interest

In this case, you have learned how to use XML Parsing in Android Application that allows you to make many features easily in your application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)