文章目录
- 一、简单介绍
- 二、使用方法
- 三、代码实现
一、简单介绍
在百度AI智能平台网站购买天文气象API服务。
购买完了以后,在控制台会看到购买信息。
二、使用方法
可通过AppCode或API签名的方式调用。两种调用方式都需要在请求中添加名为X-Bce-Signature的请求头(Header),该请求头的值AppCode或API签名的方式不同。
签名中需要用到的AppCode或AccessKey/AppSecret可在云市场的已购服务列表中已购买的API商品的订单详情页获得。
这里我们简单直接使用AppCode方式调用:
使用AppCode方式调用API较为简单,只要将AppCode以特定的格式放置于X-Bce-Signature请求头中即可。具体格式为"AppCode/" + AppCode值。如AppCode值为6f4ac66971454093bcceb34521bb541e,那么X-Bce-Signature的值为AppCode/6f4ac66971454093bcceb34521bb541e。
URL:
https://jisuweather.api.bdymkt.com/weather/query
后面拼接参数city、cityid、citycode、location等 (注意,其中一个即可,不是所有都要拼接)
例如:
String Url = "https://jisuweather.api.bdymkt.com/weather/query?location="+location+"";
或者
String Url = "https://jisuweather.api.bdymkt.com/weather/query?citycode="+citycode+"";
三、代码实现
public String getWeatherByLocaltion(String location){
String Url = "https://jisuweather.api.bdymkt.com/weather/query?location="+location+"";
StringBuffer sb = null;
try {
URL url = new URL(Url);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod("GET");
//设置请求头 后面是你的AppCode
httpUrlConn.setRequestProperty("X-Bce-Signature","AppCode/********************");
//httpUrlConn.connect(); 可要 可不要
// 获取输入流
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 读取返回结果
sb = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
httpUrlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
String str = sb.toString();
return dealWeatherStr(str);
}
public String getWeatherByCitycode(String citycode){
String Url = "https://jisuweather.api.bdymkt.com/weather/query?citycode="+citycode+"";
StringBuffer sb = null;
try {
URL url = new URL(Url);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod("GET");
//设置请求头
httpUrlConn.setRequestProperty("X-Bce-Signature","AppCode/0a7f170702e341f6b3cbd8a4e8274cb9");
//httpUrlConn.connect(); 可要 可不要
// 获取输入流
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 读取返回结果
sb = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
httpUrlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
String str = sb.toString();
return dealWeatherStr(str);
}
返回的json字符串:
{
"status":0,
"msg":"ok",
"result":{
"city":"上海",
"cityid":24,
"citycode":"101020100",
"date":"2020-10-11",
"week":"星期日",
"weather":"多云",
"temp":"23",
"temphigh":"24",
"templow":"18",
"img":"1",
"humidity":"50",
"pressure":"1014",
"windspeed":"2.9",
"winddirect":"静风",
"windpower":"0级",
"updatetime":"2020-10-11 11:35:00",
"index":[
{
"iname":"空调指数",
"ivalue":"较少开启",
"detail":"您将感到很舒适,一般不需要开启空调。"
},
{
"iname":"运动指数",
"ivalue":"适宜",
"detail":"天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。"
},
{
"iname":"紫外线指数",
"ivalue":"弱",
"detail":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"
},
{
"iname":"感冒指数",
"ivalue":"少发",
"detail":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。"
},
{
"iname":"洗车指数",
"ivalue":"较适宜",
"detail":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
},
{
"iname":"空气污染扩散指数",
"ivalue":"中",
"detail":"气象条件对空气污染物稀释、扩散和清除无明显影响,易感人群应适当减少室外活动时间。"
},
{
"iname":"穿衣指数",
"ivalue":"舒适",
"detail":"建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"
}
],
"aqi":{
"so2":"8",
"so224":"7",
"no2":"27",
"no224":"38",
"co":"0.660",
"co24":"0.560",
"o3":"104",
"o38":"51",
"o324":"58",
"pm10":"35",
"pm1024":"36",
"pm2_5":"21",
"pm2_524":"18",
"iso2":"3",
"ino2":"14",
"ico":"7",
"io3":"33",
"io38":"26",
"ipm10":"35",
"ipm2_5":"31",
"aqi":"35",
"primarypollutant":"PM10",
"quality":"优",
"timepoint":"2020-10-11 11:00:00",
"aqiinfo":{
"level":"一级",
"color":"#00e400",
"affect":"空气质量令人满意,基本无空气污染",
"measure":"各类人群可正常活动"
}
},
"daily":[
{
"date":"2020-10-11",
"week":"星期日",
"sunrise":"05:53",
"sunset":"17:28",
"night":{
"weather":"晴",
"templow":"18",
"img":"0",
"winddirect":"南风",
"windpower":"微风"
},
"day":{
"weather":"多云",
"temphigh":"24",
"img":"1",
"winddirect":"南风",
"windpower":"微风"
}
},
{
"date":"2020-10-12",
"week":"星期一",
"sunrise":"05:54",
"sunset":"17:27",
"night":{
"weather":"多云",
"templow":"19",
"img":"1",
"winddirect":"西南风",
"windpower":"微风"
},
"day":{
"weather":"多云",
"temphigh":"25",
"img":"1",
"winddirect":"东南风",
"windpower":"微风"
}
},
{
"date":"2020-10-13",
"week":"星期二",
"sunrise":"05:55",
"sunset":"17:25",
"night":{
"weather":"多云",
"templow":"18",
"img":"1",
"winddirect":"西南风",
"windpower":"微风"
},
"day":{
"weather":"多云",
"temphigh":"24",
"img":"1",
"winddirect":"南风",
"windpower":"微风"
}
},
{
"date":"2020-10-14",
"week":"星期三",
"sunrise":"05:55",
"sunset":"17:24",
"night":{
"weather":"阴",
"templow":"18",
"img":"2",
"winddirect":"西南风",
"windpower":"微风"
},
"day":{
"weather":"多云",
"temphigh":"23",
"img":"1",
"winddirect":"西南风",
"windpower":"微风"
}
},
{
"date":"2020-10-15",
"week":"星期四",
"sunrise":"05:56",
"sunset":"17:23",
"night":{
"weather":"小雨",
"templow":"17",
"img":"7",
"winddirect":"南风",
"windpower":"微风"
},
"day":{
"weather":"阴",
"temphigh":"21",
"img":"2",
"winddirect":"西南风",
"windpower":"微风"
}
},
{
"date":"2020-10-16",
"week":"星期五",
"sunrise":"05:57",
"sunset":"17:22",
"night":{
"weather":"阴",
"templow":"16",
"img":"2",
"winddirect":"西南风",
"windpower":"微风"
},
"day":{
"weather":"小雨",
"temphigh":"21",
"img":"7",
"winddirect":"西南风",
"windpower":"微风"
}
},
{
"date":"2020-10-17",
"week":"星期六",
"sunrise":"05:57",
"sunset":"17:21",
"night":{
"weather":"多云",
"templow":"18",
"img":"1",
"winddirect":"西南风",
"windpower":"微风"
},
"day":{
"weather":"阴",
"temphigh":"22",
"img":"2",
"winddirect":"西南风",
"windpower":"微风"
}
}
],
"hourly":[
{
"time":"11:00",
"weather":"多云",
"temp":"24",
"img":"1"
},
{
"time":"12:00",
"weather":"多云",
"temp":"24",
"img":"1"
},
{
"time":"13:00",
"weather":"阴",
"temp":"24",
"img":"2"
},
{
"time":"14:00",
"weather":"阴",
"temp":"24",
"img":"2"
},
{
"time":"15:00",
"weather":"阴",
"temp":"24",
"img":"2"
},
{
"time":"16:00",
"weather":"阴",
"temp":"24",
"img":"2"
},
{
"time":"17:00",
"weather":"阴",
"temp":"23",
"img":"2"
},
{
"time":"18:00",
"weather":"多云",
"temp":"23",
"img":"1"
},
{
"time":"19:00",
"weather":"多云",
"temp":"22",
"img":"1"
},
{
"time":"20:00",
"weather":"晴",
"temp":"21",
"img":"0"
},
{
"time":"21:00",
"weather":"晴",
"temp":"20",
"img":"0"
},
{
"time":"22:00",
"weather":"晴",
"temp":"20",
"img":"0"
},
{
"time":"23:00",
"weather":"晴",
"temp":"20",
"img":"0"
},
{
"time":"0:00",
"weather":"晴",
"temp":"19",
"img":"0"
},
{
"time":"1:00",
"weather":"晴",
"temp":"19",
"img":"0"
},
{
"time":"2:00",
"weather":"晴",
"temp":"19",
"img":"0"
},
{
"time":"3:00",
"weather":"晴",
"temp":"19",
"img":"0"
},
{
"time":"4:00",
"weather":"晴",
"temp":"19",
"img":"0"
},
{
"time":"5:00",
"weather":"晴",
"temp":"18",
"img":"0"
},
{
"time":"6:00",
"weather":"晴",
"temp":"19",
"img":"0"
},
{
"time":"7:00",
"weather":"晴",
"temp":"20",
"img":"0"
},
{
"time":"8:00",
"weather":"晴",
"temp":"20",
"img":"0"
},
{
"time":"9:00",
"weather":"多云",
"temp":"22",
"img":"1"
},
{
"time":"10:00",
"weather":"多云",
"temp":"24",
"img":"1"
}
]
}
}
对返回的JOSN串进行解析:
(对应的实体对象就不再一一展示,可以自己创建)
public String dealWeatherStr(String str){
WeatherResponse response = com.alibaba.fastjson.JSONObject.parseObject(str, WeatherResponse.class);
// System.out.println("weatherResponse:"+response);
WeatherResult result = com.alibaba.fastjson.JSONObject.parseObject(response.getResult(), WeatherResult.class);
// System.out.println("weatherResult:"+result);
String todayWeather ="今天是"+result.getDate()+" "+result.getWeek()+"\r\n"+result.getCity()+"今日天气:"+result.getWeather()+",最高温度:"+result.getTemphigh()+"℃"+",最低温度:"+result.getTemplow()+"℃";
List<WeatherIndex> index = com.alibaba.fastjson.JSONObject.parseArray(result.getIndex(), WeatherIndex.class);
StringBuffer sb1 = new StringBuffer();
for (WeatherIndex weatherIndex : index) {
sb1.append(weatherIndex.getIname());
sb1.append(" : ");
sb1.append(weatherIndex.getIvalue()+","+weatherIndex.getDetail());
sb1.append("\r\n");
}
// System.out.println("sb1:"+sb1);
WeatherAQI aqi = com.alibaba.fastjson.JSONObject.parseObject(result.getAqi(), WeatherAQI.class);
WeatherAQIInfo aqiInfo = com.alibaba.fastjson.JSONObject.parseObject(aqi.getAqiInfo(), WeatherAQIInfo.class);
// System.out.println("aqi:"+aqi);
// System.out.println("aqiInfo:"+aqiInfo);
String aqiStr = "空气质量信息:空气等级:"+aqiInfo.getLevel()+","+aqi.getQuality()+","+aqiInfo.getMeasure()+","+aqiInfo.getAffect();
// System.out.println(aqiStr);
List<WeatherDaily> dailies = com.alibaba.fastjson.JSONObject.parseArray(result.getDaily(), WeatherDaily.class);
StringBuffer sb2 = new StringBuffer();
for (WeatherDaily daily : dailies) {
DayAndNight day = com.alibaba.fastjson.JSONObject.parseObject(daily.getDay(), DayAndNight.class);
DayAndNight night = com.alibaba.fastjson.JSONObject.parseObject(daily.getNight(), DayAndNight.class);
sb2.append(daily.getDate()+","+daily.getWeek()+" :\r\n白天:"+day.getWeather()+",最高温度:"+day.getTemphigh()+"℃,"+day.getWinddirect()+","+day.getWindpower()+"\r\n夜间:"+night.getWeather()+",最高温度:"+night.getTemplow()+"℃,"+night.getWinddirect()+","+night.getWindpower());
sb2.append("\r\n");
}
// System.out.println("sb2:"+sb2);
// String res = todayWeather+"\r\n"+sb1+aqiStr+"\r\n"+sb2;
String res = todayWeather+"\r\n生活小贴士:\r\n"+sb1+"一周天气预报:"+"\r\n"+sb2;
System.out.println(res);
return res ;
}