android通过google api获取天气信息示例
发布时间:2023-10-18 15:44:22
标签:android,天气
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;
}
}


猜你喜欢
- 为了解放程序员的双手,减少重复性代码的编写,推荐使用插件:mybatis-plus-generator 进行代码自动生成。下面我将详细介绍通
- 前言Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存。Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案
- 1、String 是最基本的数据类型吗?不是。Java 中的基本数据类型只有 8 个 :byte、short、int、long、float、
- java对象拷贝详解及实例Java赋值是复制对象引用,如果我们想要得到一个对象的副本,使用赋值操作是无法达到目的的:@Testpublic
- 创建AlertDialog的步骤:创建AlertDialog.Builder对象调用Builder对象的setTitle方法设置标题,set
- 一.什么是SemaphoreSemaphore,俗称信号量,它是操作系统中PV操作的原语在java的实现,它也是基于AbstractQueu
- 在Update函数中执行:if (turnAround) { playerCamera.RotateA
- 一、引言在许多编程语言中,都有函数回调这一概念。C 和 C++ 中有函数指针,因此可以将函数作为参数传给其它函数,以便过后调用。而在 Jav
- 1. json数据类型类型描述Number数字型String字符串型Boolean布尔型Array数组Object对象null空值(1)js
- 本文实例讲述了C#实现缩放和剪裁图片的方法。分享给大家供大家参考,具体如下:using System;using System.Collec
- 先看看效果图:package com.fenghuo.struts.download;import java.net.URLEncoder;
- 官网教程一、翻转(镜像)头文件 quick_opencv.h:声明类与公共函数#pragma once#include <opencv
- 用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法:1):通过QrCodeNet(Gma.QrCod
- Android开发sdk过程中,很有可能在sdk内部引
- 这篇文章主要介绍了Mybatis一对多关联关系映射实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 代理模式代理模式(Proxy Pattern)是一种结构性模式。代理模式为一个对象提供了一个替身,以控制对这个对象的访问。即通过代理对象访问
- 1. 前言随着数据量和调用量的增长,用户对应用的性能要求越来越高。另外,在实际的服务中,还存在着这样的场景:系统在组装数据的时候,对于数据的
- 这篇文章主要介绍了java加载property文件配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 目录前言HuTool 中的一些常用工具类日期相关 API随机工具图片工具彩色转换成黑白添加文字水印加密解密工具布隆过滤器邮件工具HTML 工
- 一.本地Nacos安装环境: Win7 ,JDK8 ,maven3.51.下载安装包2.启动nacos服务,bin文件下下面startup.