google 天气预报API

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:

View Code
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 Code
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"";
}
}
}
posted @ 2011-12-28 10:59  囧里个囧  阅读(14219)  评论(10编辑  收藏  举报