android通过google api获取天气信息示例
作者:
这篇文章主要介绍了android通过google api获取天气信息示例,需要的朋友可以参考下
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 微信 sdk api调用不成功解决方案
- android monkey自动化测试改为java调用monkeyrunner Api
- Android 高版本API方法在低版本系统上的兼容性处理
- Android 调用百度地图API示例
- android开发教程之获取使用当前api的应用程序名称
- android通过Location API显示地址信息的实现方法
- Android通过原生APi获取所在位置的经纬度
- Android提高之蓝牙隐藏API探秘
- Android指纹识别API初试
- Android开发学习笔记之通过API接口将LaTex数学函数表达式转化为图片形式
- Android 支付宝支付、微信支付、银联支付 整合第三方支付接入方法(后台订单支付API设计)
- Android4.4 WebAPI实现拍照上传功能
- 使用android隐藏api实现亮度调节的方法
- Android API开发之SMS短信服务处理和获取联系人的方法
- Android 用 camera2 API 自定义相机
- Android基于API的Tabs3实现仿优酷tabhost效果实例
- Android 多媒体播放API简单实例
- 最新Android版本、代号、对应API/NDK级别、发布时间及市场份额