Two ways for Android to obtain Weather Forecasts (National Meteorological Administration and Central Meteorological Observatory weather forecast API)

Source: Internet
Author: User

There is a weather forecast demand in the project. I found a suggestion on the Internet for most people to use WebService to obtain data. So I tried to do it. I did not know it, but I learned a lot from it.

First, let's get the weather service provided by the National Meteorological Administration through WebService.

The android SDK does not provide a library to call WebService. Therefore, you must use a third-party SDK to call WebService. There are some sdks suitable for WebService clients on mobile phones. ksoap2 is commonly used. (We strongly recommend that you go to the official website to download http://code.google.com/p/ksoap2-android /. The blogger randomly found one before and the result was that the referenced SDK had a problem. .) Be sure to download the correct jar package.
Http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.4/ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar
Click View raw file to download the corresponding file correctly.



Okay, let's go to the code below (here is a brief introduction)


Public class myweather extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // display the specific weather forecast data showweather ("Beijing");} // display the specific weather forecast data private void showweather (string city) {string weathertoday = NULL; int icontoday [] = new int [2]; // gets the object soapobject detail = webserviceutil returned by the remote web service. getweatherbycity (city); // obtain the weather conditions of a City Based on the city // Get weather conditions weathercurrent = detail. getproperty (4 ). tostring (); // parse today's weather conditions string date = detail. getproperty (7 ). tostring (); weathertoday = "Today:" + date. split ("") [0]; weathertoday = weathertoday + "\ n weather:" + date. split ("") [1]; weathertoday = weathertoday + "\ n temperature:" + detail. getproperty (8 ). tostring (); weathertoday = weathertoday + "\ n wind power:" + detail. getproperty (9 ). tostring () + "\ n"; icontoday [0] = parseicon (detail. Getproperty (10 ). tostring (); icontoday [1] = parseicon (detail. getproperty (11 ). tostring ();} // tool method. This method converts the returned weather icon string to the program's image resource ID. Private int parseicon (string stricon) {If (stricon = NULL) Return-1; if ("0.gif ". equals (stricon) return r. drawable. a_0; If ("1.gif ". equals (stricon) return r. drawable. a_1; If ("2.gif ". equals (stricon) return r. drawable. a_2; If ("3.gif ". equals (stricon) return r. drawable. a_3; If ("4.gif ". equals (stricon) return r. drawable. a_4; If ("5.gif ". equals (stricon) return r. drawable. a_5; If ("6.gif ". equals (stricon) return r. drawable. a_6; If ("7.gif ". equals (stricon) return r. drawable. a_7; If ("8.gif ". equals (stricon) return r. drawable. a_8; If ("9.gif ". equals (stricon) return r. drawable. a_9; If ("10.gif ". equals (stricon) return r. drawable. a_10; If ("11.gif ". equals (stricon) return r. drawable. a_11; If ("12.gif ". equals (stricon) return r. drawable. a_12; If ("13.gif ". equals (stricon) return r. drawable. a_13; If ("14.gif ". equals (stricon) return r. drawable. a_14; If ("15.gif ". equals (stricon) return r. drawable. a_15; If ("16.gif ". equals (stricon) return r. drawable. a_16; If ("17.gif ". equals (stricon) return r. drawable. a_17; If ("18.gif ". equals (stricon) return r. drawable. a_18; If ("19.gif ". equals (stricon) return r. drawable. a_19; If ("20.gif ". equals (stricon) return r. drawable. a_20; If ("21.gif ". equals (stricon) return r. drawable. a_21; If ("22.gif ". equals (stricon) return r. drawable. a_22; If ("23.gif ". equals (stricon) return r. drawable. a_23; If ("24.gif ". equals (stricon) return r. drawable. a_24; If ("25.gif ". equals (stricon) return r. drawable. a_25; If ("26.gif ". equals (stricon) return r. drawable. a_26; If ("27.gif ". equals (stricon) return r. drawable. a_27; If ("28.gif ". equals (stricon) return r. drawable. a_28; If ("29.gif ". equals (stricon) return r. drawable. a_29; If ("30.gif ". equals (stricon) return r. drawable. a_30; If ("31.gif ". equals (stricon) return r. drawable. a_31; return 0 ;}}

However, after running the project multiple times, the data cannot be obtained. We found that the services provided by the China Meteorological Administration were paid and free. For free, there is a certain limit on the number of visits per day. Installing apps on other mobile phones does not help. In addition, free services are sometimes not very stable, so you can only use other services.

The following describes how to obtain weather data by accessing the weather forecast API of the Central Meteorological Observatory.


The only drawback of using this service is to get the corresponding city code, this is a bit of trouble (but you can download the http://download.csdn.net/tag/%E4%B8%AD%E5%9B%BD%E5%A4%A9%E6%B0%94%E7%BD%91%E5%9F%8E%E5%B8%82%E4%BB%A3%E7%A0%81%E6%95%B4%E7%90%86%E7%89%88.txt here) Other stability and broad is also very first-class, it can be accurate to the county and district. the following goes straight to the topic:


The request address for the weather forecast of this service is formatted as follows ):


{"Weatherinfo": {"city": "Beidaihe", "city_en": "Beidaihe", "date_y": "August July 31, 2013", "date ":"", "Week": "Wednesday", "fchh": "18", "cityid": "101091106", "temp1": "23 ℃ ~ 28 ℃ "," temp2 ":" 23 ℃ ~ 27 ℃ "," temp3 ":" 25 ℃ ~ 28 ℃ "," temp4 ":" 25 ℃ ~ 30 ℃ "," temp5 ":" 25 ℃ ~ 29 ℃ "," temp6 ":" 25 ℃ ~ 28 ℃ "," tempf1 ":" 73.4 bytes ~ 82.4 bytes "," tempf2 ":" 73.4 bytes ~ 80.6 bytes "," tempf3 ":" 77 bytes ~ 82.4 bytes "," tempf4 ":" 77 bytes ~ 86 bytes "," tempf5 ":" 77 bytes ~ 84.2 bytes "," tempf6 ":" 77 bytes ~ 82.4 Tib "," weather1 ":" clear to thundershower "," weatsp2 ":" light rain "," weather3 ":" light rain to light rain "," weather4 ":" light rain ", "weather5": "Cloudy to clear", "weather6": "clear to overcast", "img1": "0", "img2": "4", "img3 ": "7", "img4": "99", "img5": "8", "img6": "7", "img7": "7", "img8 ": "99", "img9": "1", "img10": "0", "img11": "0", "img12": "2", "img_single ": "4", "img_title1": "clear", "img_title2": "thundershower", "img_title3": "light rain", "img_title4": "light rain", "img_title5 ": "moderate rain", "img_title6": "light rain", "img_title7": "light rain", "img_titl E8 ":" light rain "," img_title9 ":" Cloudy "," img_title10 ":" clear "," img_title11 ":" clear "," img_title12 ":" Overcast ", "img_title_single": "thundershower", "wind1": "Breeze", "wind2": "Breeze", "wind3": "Level 3-4 SouthWest Wind", "wind4 ": "Breeze", "wind5": "Breeze", "wind6": "southeast to southwest 3-4", "fx1": "Breeze", "fx2": "Breeze ", "FL1": "less than 3", "fl2": "less than 3", "fl3": "3-4", "fl4": "less than 3 ", "fl5": "less than 3", "fl6": "3-4", "Index": "hot", "index_d": "hot weather, summer clothes such as short skirts, shorts, thin jackets, and T-shirts are recommended. "," Index48 ":" comfortable "," index48_d ":" long-sleeved t-shirts, shirts, single pants, and other clothes are recommended. The elderly and inner should have knitted long-sleeved shirts, vests and trousers. "," Index_uv ":" weak "," index48_uv ":" weakest "," index_xc ":" Not suitable "," index_tr ":" General "," index_co ": "Not Comfortable", "ST1": "26", "st2": "23", "st3": "25", "st4": "21 ", "ST5": "25", "st6": "21", "index_cl": "Not suitable", "index_ls": "Not suitable", "index_ag ": "extremely difficult "}}

Some parameter descriptions: temp1 ~ 6 indicates the lowest temperature and the highest temperature in six days, tempf1 ~ Tempf6: indicates the Fahrenheit temperature.
Img1 ~ Img2 indicates the weather icon for today's day and night, img3 ~ Img4 indicates the weather icon for day and night tomorrow...

Then we get these strings. We will not introduce them here.


I will introduce the jsonobject class (http://henzil.easymorse.com /? P = 242)


Jsonobject = new jsonobject (jsonstring) .. getjsonobject ("weatherinfo"); where jsonstring is the obtained string

For example, get the weather picture
String imgurl = jsonobject. getstring ("img1"); then you can obtain the image through this spelling string.
Http://m.weather.com.cn/img/ B + imgurl + ". GIF"

The above are two ways to get weather conditions. You are welcome to give your comments or suggestions.

Related Article

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.