This project is based on WebService and uses ksoap2 to parse WebServices on the network.
In fact, there are not many technical difficulties. Let's look at the source code and explain it again.
Import Java. util. arraylist; import Java. util. list; import Org. ksoap2.soapenvelope; import Org. ksoap2.serialization. soapobject; import Org. ksoap2.serialization. soapserializationenvelope; import Org. ksoap2.transport. httptransportse; public class webserviceutil {// define the Web Service namespace static final string service_ns = "http://WebXml.com.cn/"; // define the urlstatic final string service_url = "http: // Webs Ervice.webxml.com.cn/webservices/weatherws.asmx ";/*** obtain state, domestic and foreign province and city information ** @ return */public static list <string> getprovincelist () {// method name to be called (obtain information about continents, provinces, and cities supported by the weather forecast Web Services) string methodname = "getregionprovince "; // create the httptransportse transmission object httptransportse httptranstation = new httptransportse (service_url); httptranstation. DEBUG = true; // use the soap1.1 protocol to create the envelop object soapserializationenvelope envelope = ne W soapserializationenvelope (soapenvelope. ver11); // instantiate soapobject object soapobject = new soapobject (service_ns, methodname); envelope. bodyout = soapobject; // set and. the Web service provided by. Net maintains good compatibility with envelope. DOTNET = true; try {// call Web servicehttptranstation. call (service_ns + methodname, envelope); If (envelope. getresponse ()! = NULL) {// get the SOAP message soapobject result returned by the server response = (soapobject) envelope. bodyin; soapobject detail = (soapobject) result. getproperty (methodname + "result"); // parse the SOAP message returned by the server. Return parseprovinceorcity (detail) ;}} catch (exception e) {e. printstacktrace ();} return NULL;}/*** obtain the city list by province ** @ Param province * @ return */public static list <string> getcitylistbyprovince (string Province) {// method name to be called (obtain the city information supported by the weather forecast Web Services, query the city set by province: with parameters) string methodname = "getsupportcitystring "; httptransportse httptranstation = new httptransportse (service_url); httptranstation. DEBUG = true; s Oapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); soapobject = new soapobject (service_ns, methodname); soapobject. addproperty ("theregioncode", province); envelope. bodyout = soapobject; envelope. DOTNET = true; try {// call Web servicehttptranstation. call (service_ns + methodname, envelope); If (envelope. getresponse ()! = NULL) {// get the SOAP message soapobject result returned by the server response = (soapobject) envelope. bodyin; soapobject detail = (soapobject) result. getproperty (methodname + "result"); // parse the SOAP message returned by the server. Return parseprovinceorcity (detail) ;}} catch (exception e) {e. printstacktrace ();} return NULL;} Private Static list <string> parseprovinceorcity (soapobject detail) {arraylist <string> result = new arraylist <string> (); for (INT I = 0; I <detail. getpropertycount (); I ++) {string STR = detail. getproperty (I ). tostring (); // parse the result of each province. add (Str. split (",") [0]);} return result;} public static soapobject getweatherby City (string cityname) {// query the weather conditions, current weather conditions, weather and life indexes in the next three days by city or region name. String methodname = "getweather "; httptransportse httptranstation = new httptransportse (service_url); httptranstation. DEBUG = true; soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); soapobject = new soapobject (service_ns, methodname); soapobject. addproperty ("thecitycode", cityname); envelope. B Odyout = soapobject; envelope. DOTNET = true; try {// call Web servicehttptranstation. Call (service_ns + methodname, envelope); If (envelope. getresponse ()! = NULL) {// get the SOAP message soapobject result returned by the server response = (soapobject) envelope. bodyin; soapobject detail = (soapobject) result. getproperty (methodname + "result"); // parse the SOAP message returned by the server. Return detail ;}} catch (exception e) {e. printstacktrace () ;}return null ;}}
There are three methods to parse the province, city, and weather data respectively. WebService is used for data interaction.
The following is the code in the activity:
Import Java. util. list; import Org. ksoap2.serialization. soapobject; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. sharedpreferences; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. window; import android. widget. adapterview; import android. widget. adapterview. onitemselectedlis Tener; import android. widget. arrayadapter; import android. widget. button; import android. widget. imageview; import android. widget. spindle; import android. widget. textview; import android. widget. toast; import com.kang.net. webserviceutil; public class weatherwebserviceactivity extends activity {private textview text; private button city_btn; Private Static final int city = 0x11; private string city_str; private Tex Tview city_text; private spinner province_spinner; private spinner city_spinner; private list <string> provinces; private list <string> citys; private sharedpreferences preference;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. requestwindowfeature (window. feature_no_title); // remove the title bar setcontentview (R. layout. mai N); preference = getsharedpreferences ("Weather", mode_private); city_str = readsharppreference (); city_text = (textview) findviewbyid (R. id. city); city_text.settext (city_str); text = (textview) findviewbyid (R. id. test); city_btn = (button) findviewbyid (R. id. city_button); city_btn.setonclicklistener (New clickevent (); findviewbyid (R. id. content_today_layout ). getbackground (). setalpha (1, 120); findviewbyid (R. id. con Tent_small_bg1 ). getbackground (). setalpha (1, 120); findviewbyid (R. id. content_small_bg2 ). getbackground (). setalpha (1, 120); findviewbyid (R. id. content_small_bg3 ). getbackground (). setalpha (120); refresh (city_str);} class clickevent implements view. onclicklistener {@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. city_button: show_dialog (city); break; default: Break ;}} public void showtast (string strin G) {toast. maketext (weatherwebserviceactivity. this, String, 1 ). show ();} public void show_dialog (INT cityid) {Switch (cityid) {Case City: // obtain the final view = layoutinflater in city_layout.xml. from (this ). inflate (R. layout. city_layout, null); // view of the Province "spinnerprovince_spinner = (spinner. findviewbyid (R. id. province_spinner); // The City of fig = (spinner) view. findviewbyid (R. id. city_spinner); // province list PR Ovinces = webserviceutil. getprovincelist (); arrayadapter adapter = new arrayadapter (this, android. r. layout. simple_spinner_item, provinces); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); province_spinner.setadapter (adapter); // the province's spinner listener (New onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Arg0, view arg1, int position, long arg3) {citys = webserviceutil. getcitylistbyprovince (provinces. get (position); arrayadapter adapter1 = new arrayadapter (weatherwebserviceactivity. this, android. r. layout. simple_spinner_item, citys); adapter1.setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); city_spinner.setadapter (adapter1); }@ overridepublic void onnothingselected (adapterview <?> Arg0) {}}); // city_spinner.setonitemselectedlistener (New onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Arg0, view arg1, int position, long arg3) {city_str = Citys. Get (position) ;}@ overridepublic void onnothingselected (adapterview <?> Arg0) {}}); // select the city dialog box alertdialog. builder dialog = new alertdialog. builder (this); dialog. settitle ("select the city"); dialog. setview (View); dialog. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {response (city_str); writesharppreference (city_str); refresh (city_str) ;}}); dialog. setnegativebutton ("cancel", new dialoginterface. onclicklis Tener () {@ overridepublic void onclick (dialoginterface diich, int which) {dialog. dismiss () ;}}); dialog. show (); break; default: break;} protected void refresh (string city_str) {soapobject detail = webserviceutil. getweatherbycity (city_str); try {// obtain the data string date = detail from <string> moderate rain to light rain on January 1, October 13 </string>. getproperty (7 ). tostring (); // splits "light rain to light rain in December October 13" into two Arrays: String [] date_array = date. split (""); textview today_te XT = (textview) findviewbyid (R. id. today); today_text.settext (date_array [0]); // obtain the data textview city_text = (textview) findviewbyid (R. id. city_text); city_text.settext (detail. getproperty (1 ). tostring (); textview today_weather = (textview) findviewbyid (R. id. today_weather); today_weather.settext (date_array [1]); // obtain the data textview qiweng_text = (textview) findview in <string> 15 ℃/21 ℃ </string>. Byid (R. id. qiweng); qiweng_text.settext (detail. getproperty (8 ). tostring (); // obtain the data in <string> today's weather conditions: temperature: 20 ℃; wind direction/wind power: southeast wind // level 2; humidity: 79% </string>, and split it into an array textview shidu_text = (textview) findviewbyid (R. id. shidu); string date1 = detail. getproperty (4 ). tostring (); shidu_text.settext (date1.split (":") [4]); // obtain the data textview fengli_text = (textview) in <string> northeast 3-4 </string>) findviewbyid (R. id. fengli); fengli_text.sett EXT (detail. getproperty (9 ). tostring (); // obtain the data in <string> air quality: Good; ultraviolet intensity: weakest </string>, and split the data by ";", and then pass ": "Split: split twice to obtain the required data string date2 = detail. getproperty (5 ). tostring (); string [] date2_array = date2.split (";"); textview kongqi_text = (textview) findviewbyid (R. id. kongqi); kongqi_text.settext (date2_array [0]. split (":") [1]); textview zhiwai_text = (textview) findviewbyid (R. id. zhiwai); zhiwai_text.settext (date2_arra Y [1]. split (":") [1]); // set TIP data // <string> Dressing Index: cooler. It is recommended that you have a long-sleeved shirt and a pair of trousers in the Spring and Autumn transitions. The elderly and inner should have knitted long-sleeved shirts, vests and trousers. Cold index: although the temperature is suitable, but the wind is large, it is easy to catch a cold, weak friends please pay attention to appropriate protection. // Sports index: Cloudy, suitable for various internal and external household sports. Car Wash Index: Not Suitable for car wash, a small amount of water on the road surface, if you insist on scrubbed the car, you need to make psychological preparations for splashing mud. Drying index: The weather is gloomy, which is not conducive to rapid evaporation of water and is not suitable for drying. If you need to dry, select a ventilated place as much as possible. // Tourism index: Cloudy, slightly windy, but the temperature is suitable. In general, the weather is still good. This weather is suitable for traveling, so you can enjoy the scenery of nature. Road Condition Index: Cloudy, the road surface is dry, and the road condition is good. Comfort index: the temperature is suitable, and the wind power is not great. In such weather conditions, you will feel fresh and comfortable. // Air Pollution Index: meteorological conditions are conducive to air pollutant dilution, diffusion, and removal, and can be normally performed outdoors. UV Index: weather with weak ultraviolet radiation, no special protection required. If you are outdoors for a long time, it is recommended that you apply sunscreen between 8-12 SPF skin care products. </String> string [] xiaotieshi = detail. getproperty (6 ). tostring (). split ("\ n"); textview xiaotieshi_text = (textview) findviewbyid (R. id. xiaotieshi); xiaotieshi_text.settext (xiaotieshi [0]); // sets the imageview image = (imageview) findviewbyid (R. id. imageview1); int icon = parseicon (detail. getproperty (10 ). tostring (); image. setimageresource (icon); // obtain the weather condition for the next day string [] date_str = detail. getproperty (12 ). tostring (). Split (""); textview tomorrow_date = (textview) findviewbyid (R. id. tomorrow_date); tomorrow_date.settext (date_str [0]); textview tomorrow_qiweng = (textview) findviewbyid (R. id. tomorrow_qiweng); tomorrow_qiweng.settext (detail. getproperty (13 ). tostring (); textview tomorrow_tianqi = (textview) findviewbyid (R. id. tomorrow_tianqi); tomorrow_tianqi.settext (date_str [1]); imageview tomorrow_image = (imageview) f Indviewbyid (R. id. tomorrow_image); int icon1 = parseicon (detail. getproperty (15 ). tostring (); tomorrow_image.setimageresource (icon1); // obtain the weather condition for the third day string [] date_str1 = detail. getproperty (17 ). tostring (). split (""); textview afterday_date = (textview) findviewbyid (R. id. afterday_date); afterday_date.settext (date_str1 [0]); textview afterday_qiweng = (textview) findviewbyid (R. id. afterday_qiweng); afterday_qiwen G. settext (detail. getproperty (18 ). tostring (); textview afterday_tianqi = (textview) findviewbyid (R. id. afterday_tianqi); afterday_tianqi.settext (date_str1 [1]); imageview afterday_image = (imageview) findviewbyid (R. id. afterday_image); int icon2 = parseicon (detail. getproperty (20 ). tostring (); afterday_image.setimageresource (icon2); // obtain the weather condition for the fourth day string [] date_str3 = detail. getproperty (22 ). tostring (). split (" "); Textview nextday_date = (textview) findviewbyid (R. id. nextday_date); nextday_date.settext (date_str3 [0]); textview nextday_qiweng = (textview) findviewbyid (R. id. nextday_qiweng); nextday_qiweng.settext (detail. getproperty (23 ). tostring (); textview nextday_tianqi = (textview) findviewbyid (R. id. nextday_tianqi); nextday_tianqi.settext (date_str3 [1]); imageview nextday_image = (imageview) findviewbyid (R. id. n Extday_image); int icon3 = parseicon (detail. getproperty (25 ). tostring (); nextday_image.setimageresource (icon3);} catch (exception e) {showtast (detail. getproperty (0 ). tostring (). split (". ") [0]) ;}/// tool method, which 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;} public void writesharppreference (string) {sharedpreferences. editor editor = preference. edit (); Editor. putstring ("city", string); Editor. commit ();} Public String readsharppreference () {string city = preference. getstring ("city", "Wuxi"); Return city ;}}
This is just a summary of some data, mainly to help readers learn how to use the ksoap2 class. The comments in the Code are also clearly written. in fact, if you want to make good applications, you should learn how to use network resources to interact with your own applications. After all, network resources are infinite, such as XML parsing, you can also parse the XML file of the Group Buying website.
Download source code: Android Weather Forecast