本文介绍一下利用国家气象网免费的数据接口抓取3天的天气数据并展示
中国气象网的的网址
http://www.weather.com.cn/
想要用气象网的免费数据首先要去他们那申请一个授权,13年之前都是完全免费的14年才开始要授权,但是还是可以不花钱可以用来学习研究
提供一个他们的官方群具体信息可以问他们的工作人员 QQ群:337438189
在申请完授权之后就可以用他提供的appid 和key去取数据了,返回的数据是json格式的
{"c":{"c1":"101010100","c2":"beijing","c3":"北京","c4":"beijing","c5":"北京","c6":"beijing","c7":"北京","c8":"china","c9":"中国","c10":"1","c11":"010","c12":"100000","c13":"116.407526","c14":"39.904030","c15":"33","c16":"AZ9010","c17":"+8"},"f":{"f1":[{"fa":"09","fb":"03","fc":"23","fd":"18","fe":"0","ff":"0","fg":"0","fh":"0","fi":"05:43|18:45"},{"fa":"01","fb":"00","fc":"27","fd":"17","fe":"0","ff":"0","fg":"0","fh":"0","fi":"05:43|18:43"},{"fa":"00","fb":"00","fc":"29","fd":"18","fe":"0","ff":"0","fg":"0","fh":"0","fi":"05:44|18:42"}],"f0":"201409020800"}}
这个就是他们返回的json串,具体这些字母代表什么有一个说明文档
0分下载
http://download.csdn.net/detail/zhouyu7373/7852959
现在说一下拿到授权之后具体怎么操作
第一步:要用appid和key值算出地址,代码如下需要base64的加密包,可以去阿帕奇官网下载
public static byte[] getSignature(String data, String key) throws Exception {
byte[] keyBytes = key.getBytes("UTF-8");
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1);
Mac mac = Mac.getInstance(HMAC_SHA1);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
return rawHmac;
}
public static String getURL(String cityCode) {
String url = "http://webapi.weather.com.cn/data/?areaid=%s&type=%s&date=%s&appid=%s";
String fullUrl = "http://webapi.weather.com.cn/data/?areaid=%s&type=%s&date=%s&appid=%s&key=%s";
String areaId = "这里需要一个城市的编号,就是被搜索的城市";
String appId = "这里输入自己的ID,好像每天都访问次数的我就不公布我的了";
String privateKey = "这里是key,我也不写我的了";
String dateString = new SimpleDateFormat("yyyyMMddHHmm")
.format(new Date());
String typeString = "forecast3d";
String keyUrl = String.format(url, new Object[] { areaId, typeString,
dateString, appId });
String requestUrl = "";
try {
String keyString = java.net.URLEncoder
.encode(Base64.encodeBase64String(getSignature(keyUrl,
privateKey)), "UTF-8");
requestUrl = String.format(fullUrl, new Object[] { areaId,
typeString, dateString, appId.substring(0, 6), keyString });
System.out.println(requestUrl);//这里输出的就是地址,可以直接敲到浏览器上访问
} catch (Exception e) {
}
return requestUrl;
}
控制台输出的地址直接敲到浏览器上就可以看到json串了,我提供一下气象网的城市编码
http://download.csdn.net/detail/zhouyu7373/7853067
这个包里分省、市、县三个级别,根据个人需要使用就行,三个级别是分成3个表的,同样是0分下载,只为分享
我把我解析json的方法也分享给大家,写的不好不要见怪。根据不同的需求取自己需要的数据,不必纠结我取的东西是什么
package ;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import com.taiji.egis.server.util.DateUtil;
import com.taiji.egis.server.util.GeneralMethod;
import com.taiji.egis.server.util.HttpConnectUtil;
import com.taiji.egis.server.ws.weather.mapper.WeatherMapper;
import com.taiji.egis.server.ws.weather.model.City;
import com.taiji.egis.server.ws.weather.model.Weather;
import com.taiji.egis.server.ws.weather.service.WeatherService;
/**
*
* 天气预报更新服务实现类
*
* @author zhouyu
* @date 20130517
*/
public class WeatherServiceRest2Impl implements WeatherService {
<span style="white-space:pre"> </span>// private Logger logger = Logger.getLogger(WeatherServiceRest2Impl.class);
<span style="white-space:pre"> </span>private static final String HMAC_SHA1 = "HmacSHA1";
<span style="white-space:pre"> </span>private WeatherMapper weatherDao = null;
<span style="white-space:pre"> </span>public void setWeatherDao(WeatherMapper weatherDao) {
<span style="white-space:pre"> </span>this.weatherDao = weatherDao;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>private List<City> errorCtiy = new ArrayList<City>();
<span style="white-space:pre"> </span>private int flag = 0;
<span style="white-space:pre"> </span>private int verson = 0;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//private static Set<String> vaneSet = new HashSet<String>();
<span style="white-space:pre"> </span>private static HashMap<String, String> windMap = new HashMap<String, String>();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>static {
<span style="white-space:pre"> </span>windMap.put("0","微风");
<span style="white-space:pre"> </span>windMap.put("1","东北风");
<span style="white-space:pre"> </span>windMap.put("2","东风");
<span style="white-space:pre"> </span>windMap.put("3","东南风");
<span style="white-space:pre"> </span>windMap.put("4","南风");
<span style="white-space:pre"> </span>windMap.put("5","西南风");
<span style="white-space:pre"> </span>windMap.put("6","西风");
<span style="white-space:pre"> </span>windMap.put("7","西北风");
<span style="white-space:pre"> </span>windMap.put("8","北风");
<span style="white-space:pre"> </span>windMap.put("9","旋转风");<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>private static HashMap<String, String> speedMap = new HashMap<String, String>();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>static{
<span style="white-space:pre"> </span>speedMap.put("0", "小于3级");
<span style="white-space:pre"> </span>speedMap.put("1", "3-4级");
<span style="white-space:pre"> </span>speedMap.put("2", "4-5级");
<span style="white-space:pre"> </span>speedMap.put("3", "5-6级");
<span style="white-space:pre"> </span>speedMap.put("4", "6-7级");
<span style="white-space:pre"> </span>speedMap.put("5", "7-8级");
<span style="white-space:pre"> </span>speedMap.put("6", "8-9级");
<span style="white-space:pre"> </span>speedMap.put("7", "9-10级");
<span style="white-space:pre"> </span>speedMap.put("8", "10-11级");
<span style="white-space:pre"> </span>speedMap.put("9", "11-12级");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void getLatestCityWeathers() {
<span style="white-space:pre"> </span>List<City> cities = new ArrayList<City>();
<span style="white-space:pre"> </span>if (weatherDao != null) {
<span style="white-space:pre"> </span>if (errorCtiy.size() == 0) {
<span style="white-space:pre"> </span>verson = weatherDao.getDBVersion();
<span style="white-space:pre"> </span>cities = weatherDao.selectWeatherCitys();
<span style="white-space:pre"> </span>} else if (errorCtiy.size() > 0 && flag == 0) {
<span style="white-space:pre"> </span>flag += 100;
<span style="white-space:pre"> </span>cities = errorCtiy;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (cities != null && cities.size() > 0) {
<span style="white-space:pre"> </span>System.out.println("------------"
<span style="white-space:pre"> </span>+ DateUtil.getNowDateTimeStr() + ":准备更新 "
<span style="white-space:pre"> </span>+ cities.size() + " 个城市的天气预报信息----------");
<span style="white-space:pre"> </span>int validCities = 0; // 计数器,用于记录获取天气预报信息成功的城市数
<span style="white-space:pre"> </span>List<Weather> weathers = new ArrayList<Weather>();
<span style="white-space:pre"> </span>for (City city : cities) {
<span style="white-space:pre"> </span>System.out.println("------------开始获取 【"
<span style="white-space:pre"> </span>+ city.getCityName() + "】 最近3天的天气预报信息.");
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>List<Weather> cityWeatherList = (ArrayList<Weather>) getWeathersByCity(city);
<span style="white-space:pre"> </span>if (cityWeatherList != null
<span style="white-space:pre"> </span>&& cityWeatherList.size() > 0) {
<span style="white-space:pre"> </span>Map<String, Object> map = new HashMap<String, Object>();
<span style="white-space:pre"> </span>map.put("dbVersion", verson);
<span style="white-space:pre"> </span>map.put("list", cityWeatherList);
<span style="white-space:pre"> </span>int test = weatherDao.batchInsertCityWeatherRest(map);
<span style="white-space:pre"> </span>System.out.println("当前版本号"+verson +",插入返回的int值:"+test);
<span style="white-space:pre"> </span>validCities++;
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>System.out.println("------------获取【"
<span style="white-space:pre"> </span>+ city.getCityName()
<span style="white-space:pre"> </span>+ "】 天气预报信息失败,没有有效的天气预报数据.");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>} catch (Exception e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>System.out.println("------------获取 【"
<span style="white-space:pre"> </span>+ city.getCityName()
<span style="white-space:pre"> </span>+ "】 最近3天的天气预报信息时发生错误,该城市被忽略.");
<span style="white-space:pre"> </span>continue;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>System.out.println("------------实际获取 到 " + validCities
<span style="white-space:pre"> </span>+ " 个城市,共计 " + weathers.size() + " 条记录,写入数据库.");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (errorCtiy.size() > 0 && flag == 0) {
<span style="white-space:pre"> </span>getLatestCityWeathers();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>private List<Weather> getWeathersByCity(City city) {
<span style="white-space:pre"> </span>SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
<span style="white-space:pre"> </span>String result = "";
<span style="white-space:pre"> </span>JSONArray weatherArray = null;
<span style="white-space:pre"> </span>String db_update = "";
<span style="white-space:pre"> </span>JSONObject cityInfo = null;
<span style="white-space:pre"> </span>List<Weather> weatherList = new ArrayList<Weather>();
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>result = HttpConnectUtil.doGetConnection(getURL(city.getCityCode()));
<span style="white-space:pre"> </span>JSONObject resultObject = JSONObject.fromObject(result);
<span style="white-space:pre"> </span>cityInfo = resultObject.getJSONObject("c");
<span style="white-space:pre"> </span>weatherArray = resultObject.getJSONObject("f")
<span style="white-space:pre"> </span>.getJSONArray("f1");
<span style="white-space:pre"> </span>db_update = resultObject.getJSONObject("f").getString("f0")
<span style="white-space:pre"> </span>.substring(0, 8);
<span style="white-space:pre"> </span>db_update = db_update.substring(0, 4) + "-"
<span style="white-space:pre"> </span>+ db_update.substring(4, 6) + "-"
<span style="white-space:pre"> </span>+ db_update.substring(6, 8);
<span style="white-space:pre"> </span>} catch (Exception e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>errorCtiy.add(city);
<span style="white-space:pre"> </span>return weatherList;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for (int i = 0; i < 3; i++) {
<span style="white-space:pre"> </span>JSONObject weatherInfo = weatherArray.getJSONObject(i);
<span style="white-space:pre"> </span>Weather wea = new Weather();
<span style="white-space:pre"> </span>String cityName = cityInfo.getString("c3");
<span style="white-space:pre"> </span>System.out.println("气象网的城市名:"+ cityName +"数据库中的城市名:"+city.getCityName());
<span style="white-space:pre"> </span>String weather1 = weatherCast(weatherInfo.getString("fa"));
<span style="white-space:pre"> </span>String weather2 = weatherCast(weatherInfo.getString("fb"));
<span style="white-space:pre"> </span>int temperature1 = weatherInfo.getInt("fc");
<span style="white-space:pre"> </span>int temperature2 = weatherInfo.getInt("fd");
<span style="white-space:pre"> </span>String wind1 = windMap.get(weatherInfo.getString("fe"));
<span style="white-space:pre"> </span>String wind2 = windMap.get(weatherInfo.getString("ff"));
<span style="white-space:pre"> </span>String speed1 = speedMap.get(weatherInfo.getString("fg"));
<span style="white-space:pre"> </span>String speed2 = speedMap.get(weatherInfo.getString("fh"));
<span style="white-space:pre"> </span>String img1 = weatherInfo.getString("fa");
<span style="white-space:pre"> </span>String img2 = weatherInfo.getString("fb");
<span style="white-space:pre"> </span>wea.setId(GeneralMethod.getUUIDStr());
<span style="white-space:pre"> </span>wea.setCityName(city.getCityName());
<span style="white-space:pre"> </span>wea.setForetime(24 * i);
<span style="white-space:pre"> </span>wea.setWeather1(weather1);
<span style="white-space:pre"> </span>wea.setWeather2(weather2);
<span style="white-space:pre"> </span>wea.setTemperature1(temperature1);
<span style="white-space:pre"> </span>wea.setTemperature2(temperature2);
<span style="white-space:pre"> </span>wea.setVane1(wind1);
<span style="white-space:pre"> </span>wea.setVane2(wind2);
<span style="white-space:pre"> </span>wea.setSpeed1(speed1);
<span style="white-space:pre"> </span>wea.setSpeed2(speed2);
<span style="white-space:pre"> </span>wea.setImgSrc1(img1);
<span style="white-space:pre"> </span>wea.setImgSrc2(img2);
<span style="white-space:pre"> </span>wea.setCityId(city.getCode());
<span style="white-space:pre"> </span>wea.setDbUpdate(db_update);
<span style="white-space:pre"> </span>wea.setUpdateTime(DateUtil.getNowDateTimeStr());
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>String str = db_update.replace("年", "-");
<span style="white-space:pre"> </span>str = str.replace("月", "-");
<span style="white-space:pre"> </span>str = str.replace("日", "");
<span style="white-space:pre"> </span>Date dateTime = sdf.parse(str);
<span style="white-space:pre"> </span>dateTime.setDate(dateTime.getDate() + i - 1);
<span style="white-space:pre"> </span>wea.setDateTime(dateTime);
<span style="white-space:pre"> </span>} catch (ParseException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>weatherList.add(wea);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return weatherList;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static byte[] getSignature(String data, String key) throws Exception {
<span style="white-space:pre"> </span>byte[] keyBytes = key.getBytes("UTF-8");
<span style="white-space:pre"> </span>SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1);
<span style="white-space:pre"> </span>Mac mac = Mac.getInstance(HMAC_SHA1);
<span style="white-space:pre"> </span>mac.init(signingKey);
<span style="white-space:pre"> </span>byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
<span style="white-space:pre"> </span>return rawHmac;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static String getURL(String cityCode) {
<span style="white-space:pre"> </span>String url = "http://webapi.weather.com.cn/data/?areaid=%s&type=%s&date=%s&appid=%s";
<span style="white-space:pre"> </span>String fullUrl = "http://webapi.weather.com.cn/data/?areaid=%s&type=%s&date=%s&appid=%s&key=%s";
<span style="white-space:pre"> </span>String areaId = cityCode;
<span style="white-space:pre"> </span>String appId = "<span style="font-family: Arial, Helvetica, sans-serif;">填自己的</span>";
<span style="white-space:pre"> </span>String privateKey = "<span style="font-family: Arial, Helvetica, sans-serif;">填自己的</span>";
<span style="white-space:pre"> </span>String dateString = new SimpleDateFormat("yyyyMMddHHmm")
<span style="white-space:pre"> </span>.format(new Date());
<span style="white-space:pre"> </span>String typeString = "forecast3d";
<span style="white-space:pre"> </span>String keyUrl = String.format(url, new Object[] { areaId, typeString,
<span style="white-space:pre"> </span>dateString, appId });
<span style="white-space:pre"> </span>String requestUrl = "";
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>String keyString = java.net.URLEncoder
<span style="white-space:pre"> </span>.encode(Base64.encodeBase64String(getSignature(keyUrl,
<span style="white-space:pre"> </span>privateKey)), "UTF-8");
<span style="white-space:pre"> </span>requestUrl = String.format(fullUrl, new Object[] { areaId,
<span style="white-space:pre"> </span>typeString, dateString, appId.substring(0, 6), keyString });
<span style="white-space:pre"> </span>System.out.println(requestUrl);
<span style="white-space:pre"> </span>} catch (Exception e) {
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return requestUrl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static String weatherCast(String weather) {
<span style="white-space:pre"> </span>int i = Integer.parseInt(weather);
<span style="white-space:pre"> </span>String result = "";
<span style="white-space:pre"> </span>switch (i) {
<span style="white-space:pre"> </span>case 0:
<span style="white-space:pre"> </span>result = "晴";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 1:
<span style="white-space:pre"> </span>result = "多云";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 2:
<span style="white-space:pre"> </span>result = "阴";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 3:
<span style="white-space:pre"> </span>result = "阵雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 4:
<span style="white-space:pre"> </span>result = "雷阵雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 5:
<span style="white-space:pre"> </span>result = "雷阵雨伴有冰雹";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 6:
<span style="white-space:pre"> </span>result = "雨夹雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 7:
<span style="white-space:pre"> </span>result = "小雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 8:
<span style="white-space:pre"> </span>result = "中雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 9:
<span style="white-space:pre"> </span>result = "大雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 10:
<span style="white-space:pre"> </span>result = "暴雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 11:
<span style="white-space:pre"> </span>result = "大暴雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 12:
<span style="white-space:pre"> </span>result = "特大暴雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 13:
<span style="white-space:pre"> </span>result = "阵雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 14:
<span style="white-space:pre"> </span>result = "小雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 15:
<span style="white-space:pre"> </span>result = "中雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 16:
<span style="white-space:pre"> </span>result = "大雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 17:
<span style="white-space:pre"> </span>result = "暴雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 18:
<span style="white-space:pre"> </span>result = "雾";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 19:
<span style="white-space:pre"> </span>result = "冻雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 20:
<span style="white-space:pre"> </span>result = "沙尘暴";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 21:
<span style="white-space:pre"> </span>result = "小到中雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 22:
<span style="white-space:pre"> </span>result = "中到大雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 23:
<span style="white-space:pre"> </span>result = "大到暴雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 24:
<span style="white-space:pre"> </span>result = "暴雨到大暴雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 25:
<span style="white-space:pre"> </span>result = "大暴雨到特大暴雨";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 26:
<span style="white-space:pre"> </span>result = "小到中雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 27:
<span style="white-space:pre"> </span>result = "中到大雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 28:
<span style="white-space:pre"> </span>result = "大到暴雪";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 29:
<span style="white-space:pre"> </span>result = "浮沉";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 30:
<span style="white-space:pre"> </span>result = "扬沙";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 31:
<span style="white-space:pre"> </span>result = "强沙尘暴";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 53:
<span style="white-space:pre"> </span>result = "霾";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 99:
<span style="white-space:pre"> </span>result = "无";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>result = "无";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return result;
<span style="white-space:pre"> </span>}
}