Android 如何实现google天气
2011-06-18 11:36
417 查看
1、首先,在AndroidManifest.xml文件中添加权限,允许android访问internet,如下:
<uses-permission android:name="android.permission.INTERNET"/>
其次,通过经纬度获取Google天气的API。通过此API,将返回一个XML的文件,里面包含了Google提供的天气信息。
通过NodeList,获取相关节点的数据。
具体代码如下:
java代码:
public class GoogleWeather {
private Integer Latitude;//纬度
private Integer Longitude;//经度
private String url=null;
private String sTemp;//温度
GoogleWeather(Integer longi,Integer lati)
{
Latitude=lati;
Longitude=longi;
//http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996
url="http://www.google.com/ig/api?hl=en&weather=,,,"+Longitude+","+Latitude;
Log.d("log","url="+url);
}
public void getWeatherData() throws ClientProtocolException, IOException, ParserConfigurationException, FactoryConfigurationError, SAXException
{
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest Request = new HttpGet(url);
HttpResponse Response = client.execute(Request);
HttpEntity Entity = Response.getEntity();
InputStream stream = Entity.getContent();
DocumentBuilder Builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = Builder.parse(new InputSource(stream));
NodeList n = doc.getElementsByTagName("current_conditions");
Log.d("log","Node Length="+n.getLength());
for (int i = 0; i < n.getLength(); i++)//遍列current_condition所有节点
{
//获取节点的天气数据
sTemp=n.item(i).getChildNodes().item(2).getAttributes().item(0).getNodeValue();
}
Log.d("log","sTemp="+sTemp);
}
}
<uses-permission android:name="android.permission.INTERNET"/>
其次,通过经纬度获取Google天气的API。通过此API,将返回一个XML的文件,里面包含了Google提供的天气信息。
通过NodeList,获取相关节点的数据。
具体代码如下:
java代码:
public class GoogleWeather {
private Integer Latitude;//纬度
private Integer Longitude;//经度
private String url=null;
private String sTemp;//温度
GoogleWeather(Integer longi,Integer lati)
{
Latitude=lati;
Longitude=longi;
//http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996
url="http://www.google.com/ig/api?hl=en&weather=,,,"+Longitude+","+Latitude;
Log.d("log","url="+url);
}
public void getWeatherData() throws ClientProtocolException, IOException, ParserConfigurationException, FactoryConfigurationError, SAXException
{
DefaultHttpClient client = new DefaultHttpClient();
HttpUriRequest Request = new HttpGet(url);
HttpResponse Response = client.execute(Request);
HttpEntity Entity = Response.getEntity();
InputStream stream = Entity.getContent();
DocumentBuilder Builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = Builder.parse(new InputSource(stream));
NodeList n = doc.getElementsByTagName("current_conditions");
Log.d("log","Node Length="+n.getLength());
for (int i = 0; i < n.getLength(); i++)//遍列current_condition所有节点
{
//获取节点的天气数据
sTemp=n.item(i).getChildNodes().item(2).getAttributes().item(0).getNodeValue();
}
Log.d("log","sTemp="+sTemp);
}
}
相关文章推荐
- 这个开源项目展示了如何实现一个横跨各种Android平台的音乐播放器,包括手机,平板,汽车,手表,电视等。Google官方推出,跨平台开发必看项目。
- 如何利用google原生包在android平台上实现语音识别
- 如何利用google原生包在android平台上实现语音识别
- Android如何实现音频输出切换
- Android dalvik挂钩-Xposed框架如何实现挂钩
- Android应用如何监听自己是否被卸载及卸载反馈功能的实现
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- android 如何实现两个activity跳转之间加入进度条
- android UiAutomator如何利用adb install和adb uninstall实现安装和卸载
- Android中基于HTTP的通信技术(5)Google开源库 使用 Volley 实现 JSON 字符串请求
- android中如何实现mvc,用MVC进行重构
- android中如何实现UI的实时更新---需要考虑电量和流量
- Android开发笔记---如何添加一个新的activity并实现一个activity到另一个activity的转换以及关闭自身
- 教程分享:如何实现Android沉浸式状态栏——教你让你的状态栏变个色!
- Android中免Root实现Hook的Dexposed框架实现原理解析以及如何实现应用的热修复
- android如何实现账号注销功能
- 如何在Android源码里查找Java中native方法对应的C++实现
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- 如何在Android所有界面上实现水印
- Android开发 如何快速实现分享功能