Android uses Google Weather API to make its own weather applications

Source: Internet
Author: User
Tags gettext

Background
We need simple access to XML to parse XML documents. So you just have to know where the results are in the XML document code and then parse it. For example, in the following picture, we want to know about the weather in Sfax, Tunisia:

In the beginning, we need to specify the city or state where we want to know the weather.

The code is as follows Copy Code

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");
}

Then we start parsing the XML document.

The code is as follows Copy Code

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 functionality used.

The
  code is as follows copy code
public class Xmldatacollected {
    int temp= 0;
    String City=null;
    public void setcity (String c)
    {
         city= C;
   }
    public void settemp (int t)
    {
      & nbsp temp = t;
   }
    public String datatostring ()
    {
         return "in" +city+ "the" "The" "" "" "" "+ temp+" degrees ";
   }
}.

Points of interest
In this scenario, you learned how to use XML parsing in Android applications to easily make many features

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.