The super simple and practical background Implementation of weather forecast on the public platform, simple and practical weather forecast
Ultra-simple and practical background Implementation of weather forecasts on the public platform
OverviewSome time ago, I was developing my own public platform and needed to implement the weather forecast function. When I went online, I had a lot of APIs to implement the weather forecast, including: china Meteorological Administration, Yahoo, Sina, etc. The Chinese weather forecast interface needs to be encoded nationwide. Yahoo sometimes cannot access the interface. I studied the interface provided by Sina, which is relatively simple and practical. Sina weather forecast api url is http://php.weather.sina.com.cn/xml.php? City = % B1 % B1 % BE % A9 & password = DJOYnieT8234jlsK & day = 0. Transcode the city after the city. Password is fixed. 0 indicates the weather of the Day, 1 indicates the weather of the next Day, 2 indicates the weather of the third Day, and so on. The maximum value is 4. The returned XML is described as follows:
<Profiles>
<Weather>
<City> Guangzhou </city>
<Status1> clear </status1>
<Status2> clear </status2>
<Figure1> qing </figure1>
<Figure2> qing </figure2>
<Dire1 1> no continuous direction </dire1 1>
<Direction2> no continuous direction </dire2 2>
<Power1> ≤ 3 </power1>
<Power2> ≤ 3 </power2>
<Temperature1> 7 </temperature1>
<Temperature2>-5 </temperature2>
<Ssd> 0 </ssd>
<Tgd1> 7 </tgd1>
<Tgd2> 7 </tgd2>
<Zwx> 2 </zwx>
<Ktk> 7 </ktk>
<Pollution> 3 </pollution>
<Xcz> </xcz>
<Zho> </zho>
<Diy> </diy>
<Fas> </fas>
<Chy> 6 </chy>
<Zho_shuoming> none </zho_shuoming>
<Diy_shuoming> none </diy_shuoming>
<Fas_shuoming> none </fas_shuoming>
<Chy_shuoming> cotton, winter coat, leather jacket, inner shirt or wool underwear, sweater, and overlay coat </chy_shuoming>
<Pollution_l> General </pollution_l>
<Zwx_l> weak </zwx_l>
<Ssd_l> cooler </ssd_l>
<Fas_l> none </fas_l>
<Zho_l> none </zho_l>
<Chy_l> thin winter clothes </chy_l>
<Ktk_l> enable (Heating) </ktk_l>
<Xcz_l> none </xcz_l>
<Diy_l> none </diy_l>
<Pollution_s> no significant effect on air pollutant diffusion </pollution_s>
<Zwx_s> weak ultraviolet rays </zwx_s>
<Ssd_s> for elders, children, and inner, thin scarves and gloves must be worn out. </Ssd_s>
<Ktk_s> we recommend that you turn on the air conditioner </ktk_s>
<Xcz_s> none </xcz_s>
<Gm> 2 </gm>
<Gm_l> active period </gm_l>
<Gm_s> the weather is cool, the season changes to the climate, and the clothes are carefully added. It is easy to catch a cold. </gm_s>
<Yd> 5 </yd>
<Yd_l> not suitable </yd_l>
<Yd_s> although the sky is clear, it is quite cold during outdoor sports. </yd_s>
<Savedate_weather> 2013-03-01 </savedate_weather>
<Savedate_life> 2013-03-01 </savedate_life>
<Savedate_zhishu> 2013-03-01 </savedate_zhishu>
</Weather>
</Profiles>
In the label, 1 indicates daytime, and 2 indicates nighttime. |
|
<Status> <Figure> <Direction> <Power> <Temperature> <Ssd> <Ssd_l> <Ssd_s> <Tgd> <Zwx> <Zwx_l> <Zwx_s> <Ktk> <Ktk_l> <Ktk_s> <Pollution> <Pollution_l> <Pollution_s> <Xcz> <Xcz_l> <Xcz_s> <Chy> <Chy_l> <Chy_shuoming> <Gm> <Gm_l> <Gm_s> <Yd> <Yd_l> <Yd_s> <Zho> <Zho_l> <Zho_shuoming> <Diy> <Diy_l> <Diy_shuoming> <Fas> <Fas_l> <Fas_shuoming> <Savedate_weather> <Savedate_life> <Savedate_zhishu> |
Chinese weather conditions Weather conditions pinyin Wind Direction Wind level Temperature Somatosensory index value Somatosensory Index Somatosensory index description Somatosensory Temperature UV Index Value UV Index UV Index description Air conditioning index value Air conditioner Index
Air conditioner index description Pollution Index Value Pollutant Diffusion Conditions Pollution Index description Car Wash Index Value Car Wash Index Car Wash Index description Dressing Index Value Dressing Index Dressing instructions Cold index value Cold Index Cold index description Motion index value Motion Index Motion index description Weather forecast date Date of life Exponential date
|
ImplementationBelow I will post the specific implementation code. Although XML is returned, I did not use DOM4J or XStream for parsing, And I will simply use a simple regular expression, see the specific code:
1. weather Service Class
Package com. mghs. service; import java. io. unsupportedEncodingException; import java. util. regex. matcher; import java. util. regex. pattern; import com. mghs. util. getAppConfUtil; import com. mghs. util. httpRequestUtil; /*** weather forecast service ** @ author lanp * @ since 2014-7-29 20:56:16 * @ version v1.0.1 */public class WeatherService {/*** GBK encoding * @ param source * @ return */ public static String urlEncodeGBK (String source) {St Ring result = source; try {result = java.net. URLEncoder. encode (source, "GBK");} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return result;}/*** get the XML Information of the weather forecast and return * @ param source * @ return */public static String getWeatherXml (String source, int day) {String dst = null; // Assembly query address String requestUrl = "http://php.weather.sina.com.cn/xml.php? City = {keyWord} & password = DJOYnieT8234jlsK & day = "+ day; // urlEncode UTF-8 encoded requestUrl = requestUrl for the value of q. replace ("{keyWord}", HttpRequestUtil. urlEncode (source, "GBK"); dst = HttpRequestUtil. httpRequest (requestUrl); return dst ;} /*** get the weather forecast information for today, tomorrow, and the day after tomorrow and return * @ param source * @ return */public static String getWeatherInfo (String source) {StringBuffer buffer = new StringBuffer (); buffer. append (source ). append ("The weather conditions for the next three days are as follows: \ n"); for (int I = 0; I <3; I ++) {String weatherXml = getWeatherXml (source, i); if (null = weatherXml | "". equals (weatherXml) return ""; String status1 = ""; String direction1 = ""; String temperature1 = ""; String temperature2 = ""; String savedate_weather = ""; string ssd_l = ""; String yd_s = ""; Pattern p = Pattern. compile ("(. *) (<status1> )(. *?) (</Status1> )(. *) "); Matcher m = p. matcher (weatherXml); if (m. matches () status1 = m. group (3); if (null = status1 | "". endsWith (status1) return ""; p = Pattern. compile ("(. *) (<dire1 1> )(. *?) (</Dire1 1> )(. *) "); m = p. matcher (weatherXml); if (m. matches () direction1 = m. group (3); p = Pattern. compile ("(. *) (<temperature1> )(. *?) (</Temperature1> )(. *) "); m = p. matcher (weatherXml); if (m. matches () temperature1 = m. group (3); p = Pattern. compile ("(. *) (<temperature2> )(. *?) (</Temperature2> )(. *) "); m = p. matcher (weatherXml); if (m. matches () temperature2 = m. group (3); p = Pattern. compile ("(. *) (<savedate_weather> )(. *?) (</Savedate_weather> )(. *) "); m = p. matcher (weatherXml); if (m. matches () savedate_weather = m. group (3); p = Pattern. compile ("(. *) (<ssd_l> )(. *?) (</Ssd_l> )(. *) "); m = p. matcher (weatherXml); if (m. matches () ssd_l = m. group (3); p = Pattern. compile ("(. *) (<yd_s> )(. *?) (</Yd_s> )(. *) "); m = p. matcher (weatherXml); if (m. matches () yd_s = m. group (3); buffer. append (savedate_weather ). append ("\ n "). append (status1 ). append (""). append (dire1 1 ). append (""). append (temperature2 ). append ("° -"). append (temperature1 ). append ("° "). append (ssd_l ). append ("\ n "). append ("Tip :"). append (yd_s ). append ("\ n");} return (null = buffer? "": Buffer. toString ();}/*** @ param args */public static void main (String [] args) {System. out. println (getWeatherInfo ("Guangzhou "));}}
2. http Tool
Package com. mghs. util; import java. io. bufferedReader; import java. io. inputStream; import java. io. inputStreamReader; import java. io. unsupportedEncodingException; import java.net. httpURLConnection; import java.net. URL; /*** Http request tool class * @ author lanp * @ since 2014-8-24 13:30:56 * @ version v1.0.1 */public class HttpRequestUtil {/*** encoding * @ param source * @ return */ public static String urlEncode (String source, String encode) {String result = source; try {result = java.net. URLEncoder. encode (source, encode);} catch (UnsupportedEncodingException e) {e. printStackTrace (); return "0";} return result ;} /*** initiate an http request to obtain the returned result * @ param requestUrl request address * @ return */public static String httpRequest (String requestUrl) {StringBuffer buffer = new StringBuffer (); try {URL url = new URL (requestUrl); HttpURLConnection httpUrlConn = (HttpURLConnection) url. openConnection (); httpUrlConn. setDoOutput (false); httpUrlConn. setDoInput (true); httpUrlConn. setUseCaches (false); httpUrlConn. setRequestMethod ("GET"); httpUrlConn. connect (); // convert the returned input stream to the string InputStream inputStream = httpUrlConn. getInputStream (); InputStreamReader inputStreamReader = new InputStreamReader (inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader (in PutStreamReader); String str = null; while (str = bufferedReader. readLine ())! = Null) {buffer. append (str);} bufferedReader. close (); inputStreamReader. close (); // release the resource inputStream. close (); inputStream = null; httpUrlConn. disconnect ();} catch (Exception e) {System. out. println (e. getStackTrace ();} return buffer. toString ();}/*** send an http request to obtain the returned input stream * @ param requestUrl request address * @ return InputStream */public static InputStream httpRequestIO (String requestUrl) {InputStream inputStream = null; try {URL url = new URL (requestUrl); HttpURLConnection httpUrlConn = (HttpURLConnection) url. openConnection (); httpUrlConn. setDoInput (true); httpUrlConn. setRequestMethod ("GET"); httpUrlConn. connect (); // get the returned input stream inputStream = httpUrlConn. getInputStream ();} catch (Exception e) {e. printStackTrace ();} return inputStream ;}}
You are welcome to pay attention to the public platform: MyGoodHelper, which is a good helper for your travel, life, study, and entertainment. We will make progress together. OK, TKS!