Android gets weather information sample via Google API _android

Source: Internet
Author: User
Tags parse error

Android gets weather information via Google API

Copy Code code as follows:

public class Weatheractivity extends activity {
Private TextView txcity;
Private Button btnsearch;
Private Handler Weatherhandler;
Private Dialog ProgressDialog;
private timer timer;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Timer = new timer ();
Txcity = (TextView) Findviewbyid (r.id.txcity);
Btnsearch = (Button) Findviewbyid (R.id.btnsearch);
ProgressDialog = new Alertdialog.builder (this)
. Settitle ("read in Data")
. Setmessage ("Loading data, please wait")
. Create ();

Weatherhandler = new Handler () {
public void Handlemessage (msg) {
Final String CityName = Txcity.gettext (). toString (). Trim ();
Searchweather (CityName);
Progressdialog.hide ();
}
};

Btnsearch.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Progressdialog.show ();
Timer.schedule (New TimerTask () {
@Override
public void Run () {
msg = new Message ();
Msg.settarget (Weatherhandler);
Msg.sendtotarget ();
}
},100);
}
});
}
private void Searchweather (String city) {
SAXParserFactory SPF = saxparserfactory.newinstance ();
try {
SAXParser sp = Spf.newsaxparser ();
XMLReader reader = Sp.getxmlreader ();
Xmlhandler handler = new Xmlhandler ();
Reader.setcontenthandler (handler);
URL url = new URL ("http://www.google.com/ig/api?hl=zh-cn&weather=" +urlencoder.encode (city));
InputStream is = Url.openstream ();
InputStreamReader ISR = new InputStreamReader (IS, "GBK");
InputSource Source = new InputSource (ISR);
Reader.parse (source);
List<weather>weatherlist = Handler.getweatherlist ();
Tablelayout table = (tablelayout) Findviewbyid (r.id.table);
Table.removeallviews ();
for (Weather weather:weatherlist) {
TableRow row = new TableRow (this);
Row.setlayoutparams (New Layoutparams (layoutparams.fill_parent,layoutparams.wrap_content));
Row.setgravity (gravity.center_vertical);
ImageView img = new ImageView (this);
Img.setimagedrawable (LoadImage (Weather.getimageurl ()));
Img.setminimumheight (80);
Row.addview (IMG);
TextView day = new TextView (this);
Day.settext (Weather.getday ());
Day.setgravity (gravity.center_horizontal);
Row.addview (day);
TextView temp = new TextView (this);
Temp.settext (weather.getlowtemp () + "℃-" +weather.gethightemp () + "℃");
Temp.setgravity (gravity.center_horizontal);
Row.addview (temp);
TextView condition = new TextView (this);
Condition.settext (Weather.getcondition ());
Condition.setgravity (gravity.center_horizontal);
Row.addview (condition);
Table.addview (row);
}
catch (Exception e) {
E.printstacktrace ();
New Alertdialog.builder (This)
. Settitle ("Parse error")
. Setmessage ("Failed to get weather data, please try again later.") ")
. Setnegativebutton ("OK", NULL)
. Show ();
}

}
Private drawable LoadImage (String imageUrl) {
try {
Return Drawable.createfromstream (InputStream) New URL ("http://www.google.com/" +imageurl). GetContent (), "test");
catch (Malformedurlexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
return null;
}
}

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.