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解析xml
try {
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;
}
}
}
詳細代碼:

package 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解析xml
try {
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"";
}
}
}