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