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

时间:2023-10-18 15:44:22 

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

标签:android,天气
0
投稿

猜你喜欢

  • Java简单实现定时器

    2023-07-16 18:10:58
  • Kotlin协程之Flow基础原理示例解析

    2021-10-17 21:07:44
  • MyBatis常用标签以及使用技巧总结

    2022-02-27 20:52:14
  • java垃圾回收原理之GC算法基础

    2023-10-05 16:10:09
  • Android倒计时的开始与停止 剩余时分秒的展示

    2023-07-20 03:11:31
  • c# Thread类的用法详解

    2023-02-08 06:53:28
  • C# 使用WPF 用MediaElement控件实现视频循环播放

    2022-04-28 03:34:26
  • 简单了解4种分布式session解决方案

    2023-08-09 11:45:49
  • Java NIO实现多人聊天室

    2022-04-05 09:03:17
  • MAC配置java+jmeter环境变量过程解析

    2021-09-30 00:16:23
  • Android常用三方库混淆规则整理(小结)

    2022-09-10 16:35:34
  • Android编程实现图片放大缩小功能ZoomControls控件用法实例

    2022-07-16 22:01:11
  • SpringBoot自定义MessageConvert详细讲解

    2023-04-23 19:24:39
  • 深入理解spring boot 监控

    2023-11-04 02:25:15
  • java多线程编程之join方法的使用示例

    2022-10-06 05:09:38
  • Springboot-注解-操作日志的实现方式

    2023-10-10 13:54:35
  • 如何使用正则表达式判断邮箱(以C#为例)

    2022-12-09 21:38:30
  • C#生成Word文件(图片、文字)

    2023-03-28 04:07:58
  • C#中定时任务被阻塞问题的解决方法

    2023-10-27 00:56:02
  • Android中volley封装实践记录

    2021-07-05 19:55:03
  • asp之家 软件编程 m.aspxhome.com