WeChat Baidu weather Query

Source: Internet
Author: User

I want to make a weather forecast query function and find that Baidu api is quite useful. Brief Introduction

/** * WxDaoImpl *  * @author xuyw * @email xyw10000@163.com * @date 2014-06-22 */

  1. 1. Interface Description
  2. 2. interface Example
  3. 3. interface parameter description
  4. 4. Return results
  5. 5. return data in xml format
  6. 6. return data in json format
Interface Description

Query weather results by longitude and latitude/city name

Interface Example
Http://api.map.baidu.com/telematics/v3/weather? Location = Beijing & output = json & ak = yourkey
Interface parameter description
Parameter type Parameter Name Required? Description
String Ak True Developer key
String Sn False This parameter is required if the accesskey used is used for sn verification. (Sn generation algorithm)
String Location True Enter the city name or latitude and longitude, city name such as Beijing, longitude and latitude format is lng, lat coordinates such as: location = 116.305145, 39.982368; city weather forecast center "|" separated, location = 116.305145, 39.982368 | 122.305145, 36.982368 | ....
String Output False The output data format. The default format is xml. When the output is set to 'json', the output data is in json format;
String Coord_type False Request Parameter coordinate type. The default value is gcj02 longitude and latitude coordinates. The allowed values are bd09ll, bd09mc, gcj02, and wgs84. Bd09ll indicates the coordinates of Baidu longitude and latitude, bd09mc indicates the coordinates of Baidu mocato, and gcj02 indicates the coordinates encrypted by the national test Bureau. Wgs84 indicates the coordinates obtained by gps.
Returned results
Parameter Name Description Description
CurrentCity Current City Return city name
Status Returned result status information  
Date Current Time Year-month-day
Results Weather forecast information You can return the weather conditions of the past three days (today, tomorrow, and the day after tomorrow) and the weather conditions of the past four days (today, tomorrow, the day after tomorrow, and the day after tomorrow) at night)
Results. currentCity Current City  
Results. weather_data Weather_data.date Weather Forecast Time  
Weather_data.dayPictureUrl Daytime weather forecast image url  
Weather_data.nightPictureUrl Night weather forecast image url  
Weather_data.weather Weather conditions All weather conditions ("|" separator ): clear | cloudy | overcast | shower | thundershower with hail | sleet | light rain | moderate rain | heavy rain | light snow | moderate snow | heavy snow | Fog | freezing rain | sandstorm | light rain to moderate rain | moderate rain to heavy rain | heavy rain to heavy rain | light snow to moderate snow | moderate snow to heavy snow | heavy snow to heavy snow | dust | yangsha | Strong Sandstorm | haze
Weather_data.wind Wind Power  
Weather_data.temperature Temperature  
Returns data in xml format.
<CityWeatherResponse> <status> success </status> <date> 2013-07-18 </date> <results> <currentCity> Beijing </currentCity> <weather_data> <date> Wednesday (today, real-time: 24 ℃) </date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather> cloudy </weather> <wind> breeze </wind> <temperature> 23 ℃ ~ 15 deg C </temperature> <date> Thursday </date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/leizhenyu.png </dayPictureUrl> http://api.map.baidu.com/images/weather/night/zhongyu.png </nightPictureUrl> <weather> thundershower to moderate rain </weather> <wind> breeze </wind> <temperature> 29 ~ 22 deg C </temperature> <date> Friday </date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/yin.png </dayPictureUrl> <nihtpictureurl> http://api.map.baidu.com/images/weather/night/duoyun.png </nihtpictureurl> <weather> overcast to cloudy </weather> <wind> breeze </wind> <temperature> 31 ~ 23 deg C </temperature> <date> Saturday </date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nihtpictureurl> http://api.map.baidu.com/images/weather/night/duoyun.png </nihtpictureurl> <weather> cloudy </weather> <wind> breeze </wind> <temperature> 31 ~ 24 deg C </temperature> </weather_data> <currentCity> Hefei </currentCity> <weather_data> <date> Wednesday </date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather> cloudy </weather> <wind> Dongfeng 3-4 </wind> <temperature> 27 ℃ </temperature> <date> Thursday </date> http://api.map.baidu.com/images/we <dayPictureUrl> Ather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather> cloudy </weather> <wind> northeast 3-4 </wind> <temperature> 35 ~ 27 deg C </temperature> <date> Friday </date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nightPictureUrl> http://api.map.baidu.com/images/weather/night/duoyun.png </nightPictureUrl> <weather> cloudy </weather> <wind> Nanfeng </wind> <temperature> 35 ~ 27 deg C </temperature> <date> Saturday </date> <dayPictureUrl> http://api.map.baidu.com/images/weather/day/duoyun.png </dayPictureUrl> <nihtpictureurl> http://api.map.baidu.com/images/weather/night/duoyun.png </nihtpictureurl> <weather> cloudy </weather> <wind> Dongfeng </wind> <temperature> 34 ~ 27 ℃ </temperature> </weather_data> </results> </CityWeatherResponse> <weather> shower </weather> <wind> northeast wind 3-4 </wind> <temperature> 18 ℃ </temperature> </result> </results> </CityWeatherResponse>
Return data in json format
{Error: 0, status: "success", date: "2013-07-17", results: [{currentCity: "Beijing", weather_data: [{date: "Wednesday (today, real-time: ", dayPictureUrl:" http://api.map.baidu.com/images/weather/day/duoyun.png ", nightPictureUrl:" http://api.map.baidu.com/images/weather/night/duoyun.png ", weather:" Cloudy ", wind:" Breeze ", temperature:" 23 deg C ~ 15 deg C "}, {date:" tomorrow (Thursday) ", dayPictureUrl:" http://api.map.baidu.com/images/weather/day/leizhenyu.png ", nightPictureUrl:" http://api.map.baidu.com/images/weather/night/zhongyu.png ", weather:" thundershower to moderate rain ", wind: "Breeze", temperature: "29 ~ 22 deg C "}, {date:" The day after tomorrow (Friday) ", dayPictureUrl:" http://api.map.baidu.com/images/weather/day/yin.png ", nightPictureUrl:" http://api.map.baidu.com/images/weather/night/duoyun.png ", weather:" Overcast to cloudy ", wind: "Breeze", temperature: "31 ~ 23 deg C "}, {date:" day after tomorrow (Saturday) ", dayPictureUrl:" http://api.map.baidu.com/images/weather/day/duoyun.png ", nightPictureUrl:" http://api.map.baidu.com/images/weather/night/duoyun.png ", weather:" Cloudy ", wind:" Breeze ", temperature: "31 ~ 24 deg C "}] },{ currentCity:" ", weather_data: [{date:" today (Wednesday) ", dayPictureUrl:" http://api.map.baidu.com/images/weather/day/duoyun.png ", nightPictureUrl:" http://api.map.baidu.com/images/weather/night/duoyun.png ", weather: "Cloudy", wind: "Dongfeng 3-4", temperature: "27 ℃"}, {date: "tomorrow (Thursday)", dayPictureUrl: "http://api.map.baidu.com/images/weather/day/duoyun.png", nightPictureUrl: http://api.map.baidu.com/images/weather /Night/duoyun.png ", weather:" Cloudy ", wind:" Northeast 3-4 ", temperature:" 35 ~ 27 deg C "},{ date:" The day after tomorrow (Friday) ", dayPictureUrl:" http://api.map.baidu.com/images/weather/day/duoyun.png ", nightPictureUrl:" http://api.map.baidu.com/images/weather/night/duoyun.png ", weather:" Cloudy ", wind:" Nanfeng ", temperature: "35 ~ 27 deg C "}, {date:" day after tomorrow (Saturday) ", dayPictureUrl:" http://api.map.baidu.com/images/weather/day/duoyun.png ", nightPictureUrl:" http://api.map.baidu.com/images/weather/night/duoyun.png ", weather:" Cloudy ", wind:" Dongfeng ", temperature: "34 ~ 27 ℃ "}]}
Write code

/*** Get weather ** @ param city * @ return */public static JSONObject getWeatherInfo (String city) {JSONObject obj = JSONObject. fromObject (HttpUtil. getRequest (GET_WEATHER_URL, "location:" + city, "output: json", "ak:" + Config. BAIDU_GEOCONV_KEY); return obj ;}

Returned results

{"Error": 0, "status": "success", "date": "2014-06-23", "results": [{"currentCity": "leping ", "weather_data": [{"date": "Monday June 23 (Real-Time: 25 ℃)", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhenyu.png", "maid": "http://api.map.baidu.com/images/weather/night/zhenyu.png ", "weather": "Shower", "wind": "Breeze", "temperature": "26 ~ 22 deg C "},{" date ":" Tuesday "," dayPictureUrl ":" http://api.map.baidu.com/images/weather/day/zhenyu.png "," nightPictureUrl ":" http://api.map.baidu.com/images/weather/night/zhenyu.png "," weather ":" Shower ", "wind": "Breeze", "temperature": "25 ~ 22 deg C "},{" date ":" Wednesday "," dayPictureUrl ":" http://api.map.baidu.com/images/weather/day/zhenyu.png "," nightPictureUrl ":" http://api.map.baidu.com/images/weather/night/leizhenyu.png "," weather ":" shower to thundershower ", "wind": "Breeze", "temperature": "27 ~ 23 deg C "},{" date ":" Thursday "," dayPictureUrl ":" http://api.map.baidu.com/images/weather/day/leizhenyu.png "," nightPictureUrl ":" http://api.map.baidu.com/images/weather/night/zhenyu.png "," weather ":" thundershower to shower ", "wind": "Breeze", "temperature": "29 ~ 23 ℃ "}]}

Convert the result to multiple images

/*** Obtain weather ** @ param map * @ return */public String getWeather (Map map) {String message = null; String openid = map. get ("FromUserName") + ""; String toUserName = map. get ("ToUserName") + ""; String city = map. get ("city") + ""; JSONObject obj = BaiduUtil. getWeatherInfo (city); String status = obj. getString ("status"); if ("success ". equals (status) {// found JSONArray jarray = obj. getJSONArray ("results"); int size = jarr Ay. size (); if (size> 0) {JSONObject j2 = jarray. getJSONObject (0); JSONArray jarray2 = j2.getJSONArray ("weather_data"); List <Article> articleList = new ArrayList <Article> (); // create a text message NewsMessage newsMessage = new NewsMessage (); newsMessage. setToUserName (openid); newsMessage. setFromUserName (toUserName); newsMessage. setCreateTime (new Date (). getTime (); newsMessage. setMsgType (MessageUtil. RESP_MESSAGE_TYPE_NEWS); news Message. setFuncFlag (0); int size2 = jarray2.size (); for (int I = 0; I <jarray2.size (); I ++) {JSONObject j3 = jarray2.getJSONObject (I ); string date = j3.getString ("date"); String weather = j3.getString ("weather"); String wind = j3.getString ("wind"); String temperature = j3.getString ("temperature "); string title = ""; String img_url = j3.getString ("dayPictureUrl"); if (I = 0) {title = date + "" + weather + "" + wind ;} else {title = date + "" + Weather + "" + temperature + "" + wind;} Article article = new Article (); article. setTitle (title); article. setDescription (""); article. setPicUrl (img_url); article. setUrl ("http://blog.csdn.net/xuyw10000"); articleList. add (article);} newsMessage. setArticleCount (size2); newsMessage. setArticles (articleList); message = MessageUtil. newsMessageToXml (newsMessage);} else {map. put ("xuywMsg", "Ah, the weather of" + city + "cannot be found. "); Message = sendTextMessage (map) ;}} else {// No result map. put (" xuywMsg "," Ah, Baidu cannot find this place. "); Message = sendTextMessage (map);} return message ;}

Result

<Xml> <ToUserName> <! [CDATA [sadsdaewwaewea]> </ToUserName> <FromUserName> <! [CDATA [bbbbb]> </FromUserName> <CreateTime> <! [CDATA [1403495665819]> </CreateTime> <MsgType> <! [CDATA [news]> </MsgType> <FuncFlag> <! [CDATA [0]> </FuncFlag> <ArticleCount> <! [CDATA [4]> </ArticleCount> <Articles> <item> <Title> <! [CDATA [Monday June 23 (Real-Time: 25 ℃) shower breeze]> </Title> <Description> <! [CDATA []> </Description> <PicUrl> <! [CDATA [http://api.map.baidu.com/images/weather/day/zhenyu.png#]> </PicUrl> <Url> <! [CDATA [http://blog.csdn.net/xuyw10000]]> </Url> </item> <Title> <! [CDATA [Tuesday shower 25 ~ 22 ℃ breeze]> </Title> <Description> <! [CDATA []> </Description> <PicUrl> <! [CDATA [http://api.map.baidu.com/images/weather/day/zhenyu.png#]> </PicUrl> <Url> <! [CDATA [http://blog.csdn.net/xuyw10000]]> </Url> </item> <Title> <! [CDATA [Sunday shower to thundershower 27 ~ 23 ℃ breeze]> </Title> <Description> <! [CDATA []> </Description> <PicUrl> <! [CDATA [http://api.map.baidu.com/images/weather/day/zhenyu.png#]> </PicUrl> <Url> <! [CDATA [http://blog.csdn.net/xuyw10000]]> </Url> </item> <Title> <! [CDATA [Thursday thundershower to shower 29 ~ 23 ℃ breeze]> </Title> <Description> <! [CDATA []> </Description> <PicUrl> <! [CDATA [http://api.map.baidu.com/images/weather/day/leizhenyu.png.pdf]> </PicUrl> <Url> <! [CDATA [http://blog.csdn.net/xuyw10000]]> </Url> </item> </Articles> </xml>
HttpUtil see http://blog.csdn.net/xuyw10000/article/details/33342489

Run after SAES deployment




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.