Android 如何实现google天气

    1、首先,在AndroidManifest.xml文件中添加权限,允许android访问internet,如下:


         <uses-permission android:name="android.permission.INTERNET"/> 

  1. 其次,通过经纬度获取Google天气的API。通过此API,将返回一个XML的文件,里面包含了Google提供的天气信息。
  2. 通过NodeList,获取相关节点的数据。
  3. 具体代码如下:

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

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值