欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android通过google api获取天气信息示例

程序员文章站 2024-02-03 14:49:22
android通过google api获取天气信息 复制代码 代码如下:public class weatheractivity extends activity {&n...

android通过google api获取天气信息

复制代码 代码如下:

public class weatheractivity extends activity {
 private textview txcity;
 private button btnsearch;
 private handler weatherhandler;
 private dialog progressdialog;
 private timer timer;
    /** called when the activity is first created. */
    @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("读取数据中")
        .setmessage("正在加载数据,请稍等")
        .create();

        weatherhandler = new handler(){
         public void handlemessage(message 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() {
      message 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("解析错误")
    .setmessage("获取天气数据失败,请稍候再试。")
    .setnegativebutton("确定", 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;
 }
}