google 天气预报API
来源:互联网 发布:卖家开淘宝好吗 编辑:程序博客网 时间:2024/06/08 07:36
http://www.cnblogs.com/ylligang/articles/2304434.html
Google Weather API 只支持美国地区使用邮政编码进行查询,例如:
http://www.google.com/ig/api?hl=zh-cn&weather=94043
(94043 为 山景城, 美国加州 的邮政编码)
而除了美国以外的地区需要使用经纬度坐标作为参数才能执行 Google Weather API, 例如:
http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996
(30670000,104019996 为 成都, 中国大陆 的经纬度坐标)
当然,也可能通行城市名称的汉语拼音来查询,例如:以下是北京的天气
http://www.google.com/ig/api?weather=Beijing
要其它地区的经纬度坐标,可以通过 Google API 提供的国家代码列表及相应的城市经纬度坐标列表可以查询到,以下是 Google API 提供的查询参数:
http://www.google.com/ig/countries?output=xml&hl=zh-cn
(查询 Google 所支持的所有国家的代码,并以 zh-cn 简体中文显示)
http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn
----------------------------------------------------------------------------------------------------------------------------------
Google开放了一套天气预报API,还是很好用的。
使用邮政编码(美国)
http://www.google.com/ig/api?hl=zh-cn&weather=94043(加州山景城)
使用经度纬度坐标
http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996(成都)
使用通行城市名称
http://www.google.com/ig/api?weather=Beijing&hl=zh-cn(北京)
http://www.google.com/ig/api?weather=Osaka&hl=zh-cn(大阪)
或
http://www.google.com/ig/api?weather=Beijing&hl=zh(北京)
http://www.google.com/ig/api?weather=Osaka&hl=ja(大阪)
可以查找到哪些国家和城市呢?谷歌也提供了接口。返回的类型也可以根据output参数来指定。
查找国家 http://www.google.com/ig/countries?output=xml&hl=zh-cn (返回xml)
查找城市 http://www.google.com/ig/cities?hl=zh-cn&country=cn (返回json)
有了这些数据,在自己的应用里加入天气预报就不难了。
-----------------------------------------------------------------------------------------------------------------------------------
android 通过Google Weather Api 获取天气预报
获取天气的链接地址
根据经纬度获取:http://www.google.com/ig/api?weather=,,,31174165,121433841
【如中山的经纬度是:22.516997123628076,113.39263916015625 必须都乘以1000000才能作为参数】
int a=(int)22.516997123628076*1000000;int b=(int)113.39263916015625*1000000;String strUrl ="http://www.google.com/ig/api?hl=zh-cn&weather=,,,"+a+","+b;System.out.println(strUrl);
根据地名获取:http://www.google.com/ig/api?hl=zh-cn&weather=Beijing
String strData =""; String strUrl ="http://www.google.com/ig/api?hl=zh-cn&weather=ZhongShan"; strData = getResponse(strUrl); // 天气预报的xml存储在sd卡中new FileUnit().write(strData, "weather.xml"); // SAX解析xmltry { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); SAXReader saxReader =new SAXReader(); InputSource is =new InputSource(); is.setByteStream(new ByteArrayInputStream(strData.getBytes())); sp.parse(is, saxReader); weatherList=saxReader.getWeathList(); } catch (Exception e) { e.printStackTrace(); } //显示天气预报 showWeather();
根据地址 获得xml的String
protected String getResponse(String queryURL) { URL url; try { url =new URL(queryURL.replace("", "%20")); URLConnection urlconn = url.openConnection(); urlconn.connect(); InputStream is = urlconn.getInputStream(); BufferedInputStream bis =new BufferedInputStream(is); ByteArrayBuffer buf =new ByteArrayBuffer(50); int read_data =-1; while ((read_data = bis.read()) !=-1) { buf.append(read_data); } // String resp = buf.toString(); String resp = EncodingUtils.getString(buf.toByteArray(), "GBK"); return resp; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); return""; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return""; } }
显示天气
privatevoid showWeather(){ List<String> weather=null; String strTemp=""; for(int i=0; i<weatherList.size();i++) { weather=weatherList.get(i); strTemp+="\n==========================\n"; for(int j=0;j<weather.size();j++) { strTemp+=weather.get(j)+"\n"; } } tvShow.setText(strTemp); }
SAXReader:
package com.ReadOrder;import java.util.ArrayList;import java.util.List;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;publicclass SAXReader extends DefaultHandler { privatefinal String FORECASE_INFORMATION="forecast_information"; privatefinal String CURRENT_CONDITIONS="current_conditions"; privatefinal String FORECAST_CONDITIONS="forecast_conditions"; private List<List<String>> weatherList =null; private List<String> weather =null; privatestatic String tagname =""; @Override publicvoid startDocument() throws SAXException { weatherList =new ArrayList<List<String>>(); } @Override publicvoid startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (localName.equals(FORECASE_INFORMATION) ||localName.equals(CURRENT_CONDITIONS) ||localName.equals(FORECAST_CONDITIONS)) { tagname ="current_conditions"; weather =new ArrayList<String>(); } else { if (tagname.equals(CURRENT_CONDITIONS)&& attributes.getValue("data") !=null) { weather.add (attributes.getValue("data")); System.out.println("###"+ attributes.getValue("data")); } } } @Override publicvoid endElement(String uri, String localName, String qName) throws SAXException { if (localName.equals(FORECASE_INFORMATION) ||localName.equals(CURRENT_CONDITIONS) ||localName.equals(FORECAST_CONDITIONS)) { weatherList.add(weather); weather=null; tagname=""; } } @Override publicvoid endDocument() throws SAXException { } public List<List<String>> getWeathList() { if(weatherList==null||weatherList.size()<1) { returnnull; } else { for(int i=0;i<weatherList.size();i++) { System.out.println(weatherList.get(i)); } return weatherList; } }}
详细代码:
View Codepackage com.ReadOrder;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import javax.crypto.spec.IvParameterSpec;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.apache.http.util.ByteArrayBuffer;import org.apache.http.util.EncodingUtils;import org.w3c.dom.ls.LSException;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import android.R.integer;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;publicclass GoogleWeatherActivity extends Activity { TextView tvShow; List<List<String>> weatherList=null; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("获取天气预报"); setContentView(R.layout.layout_weather); tvShow = (TextView) findViewById(R.id.TextView001); tvShow.setText("None Data"); findViewById(R.id.btnGetWeather).setOnClickListener( new OnClickListener() { @Override publicvoid onClick(View arg0) { System.out.println("btnGetWeather===>onclick"); String strData =""; String strUrl ="http://www.google.com/ig/api?hl=zh-cn&weather=ZhongShan"; strData = getResponse(strUrl); // 天气预报的xml存储在sd卡中new FileUnit().write(strData, "weather.xml"); // SAX解析xmltry { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); SAXReader saxReader =new SAXReader(); InputSource is =new InputSource(); is.setByteStream(new ByteArrayInputStream(strData.getBytes())); sp.parse(is, saxReader); weatherList=saxReader.getWeathList(); } catch (Exception e) { e.printStackTrace(); } //显示天气预报 showWeather(); } }); } privatevoid showWeather(){ List<String> weather=null; String strTemp=""; for(int i=0; i<weatherList.size();i++) { weather=weatherList.get(i); strTemp+="\n==========================\n"; for(int j=0;j<weather.size();j++) { strTemp+=weather.get(j)+"\n"; } } tvShow.setText(strTemp); } protected String getResponse(String queryURL) { URL url; try { url =new URL(queryURL.replace("", "%20")); URLConnection urlconn = url.openConnection(); urlconn.connect(); InputStream is = urlconn.getInputStream(); BufferedInputStream bis =new BufferedInputStream(is); ByteArrayBuffer buf =new ByteArrayBuffer(50); int read_data =-1; while ((read_data = bis.read()) !=-1) { buf.append(read_data); } // String resp = buf.toString(); String resp = EncodingUtils.getString(buf.toByteArray(), "GBK"); return resp; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); return""; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return""; } }}
- google 天气预报API
- Google API 获取天气预报信息
- php调用google api 天气预报
- Google天气预报API的使用
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API查询天气预报
- 使用Google Weather API 制作天气预报应用
- Google开放了一套天气预报API
- 使用Google Weather API查询天气预报
- Google 天气预报 英语版本 调用google weather api
- ubuntu 12.04 安装sun-java6-jdk
- StringUtil
- oracle判断和循环语句语法
- flex结构树实现
- 转:C# 中的委托和事件
- google 天气预报API
- 指针
- 通过url打开app的方法
- 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
- Spring学习笔记
- MySQL索引简介
- 计算机视觉领域稍微容易中的期刊(第一版)
- DPI接口的屏,PCLK、VSYNC、HSYNC以及DE的极性设置
- 《Practical WPF Charts and Graphics 》翻译——之11章 曲线拟合(1)