In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how android uses soap protocol to access webservice to achieve weather forecasting function". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how android uses soap protocol to access webservice to achieve weather forecasting function".
First of all, create a layout file to show the weather you need to look for, and you can find out what the weather will be like today, tomorrow or the day after tomorrow. To display the information you need, if you want to display the city pictures you are looking for, you should store all the city photos named after the city code in the project. There is a large amount of data, so here are only 32 pictures showing the weather. You can download them online, and remember that the order must not be wrong.
Layout file main.xml
two。 Then write activity code, because you want to use the Soap protocol, you first have to go to the website to download the soap jar package, which is used here. Because of the higher version, paste the jar package into the project libs file, the environment will automatically match and load the jar package, and will find the soap package under the dependencies under the project.
3. Note that if you want to use online resources to parse xml, set user permissions in AndroidManifest.xml
4. Write a utility class that sends weather forecast requests using web services over the soap protocol and returns results.
Get the weather conditions
(1) the getResponse () method checks the weather forecast to get a series of values, not a single value, so the returned result is received by SoapObject, and the required information is obtained by calling its getProperty (). Here are 23 attributes of getWeatherbyCityNameResult:
(2) the values of these attributes in an array can also be obtained in another way:
SoapObject result = (SoapObject) envelope.bodyIn
SoapObject detail = (SoapObject) result.getProperty ("getWeatherbyCityNameResult")
Code:
Public class WeatherStateSearch {public static SoapObject searchWea (String wsdlurl,String method,String cityname) {/ / specify the namespace of the webservice and the method name of the call String namespace= "http://WebXml.com.cn/"; SoapObject soap=new SoapObject (namespace,method) / / add attributes. As long as the order of setting parameters is the same, the parameter name of the calling method is not necessarily the same as the parameter name of the method in the server's WebService class ("theCityName", cityname); / / set the version number of the SOAP protocol through the constructor of the SoapSerializationEnvelope class. SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope (SoapEnvelope.VER11); / / set the outgoing Soap soapEnvelope.bodyOut=soap; soapEnvelope.dotNet=true; soapEnvelope.setOutputSoapObject (soap); / / create http transport object HttpTransportSE transportSE=new HttpTransportSE (wsdlurl); / / soap operation url String SOAP_ACTION=namespace+method Try {/ / request to call WebService method transportSE.call (SOAP_ACTION, soapEnvelope); / / use getResponse to get the result of parsing xml returned by WebService method SoapObject result= (SoapObject) soapEnvelope.getResponse () If (resulting null) return result;} catch (IOException e) {e.printStackTrace ();} catch (XmlPullParserException e) {e.printStackTrace ();} return null;}}
5. The key code for the project main class:
Web server weather forecast url: "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
Public class MainActivity extends Activity {private EditText city; private Button search; private EditText weastate; private ImageView img; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / find component city= (EditText) this.findViewById (R.id.city); search= (Button) this.findViewById (R.id.search) Weastate= (EditText) this.findViewById (R.id.state); img= (ImageView) this.findViewById (R.id.image); search.setOnClickListener (new OnClickListener () {public void onClick (View v) {String cname=city.getText () .toString () / / web server weather forecast url String wsdlUrl= "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; / / call the method SoapObject weather=WeatherStateSearch.searchWea provided by web (wsdlUrl," getWeatherbyCityName ", cname) String state=weather.getProperty (10). ToString (); System.out.println (state); String strIcon=weather.getProperty (15). ToString () / / query results are displayed in the result text field weastate.setText (state); / / set the weather picture img.setImageResource (parseIcon (strIcon));}}) } / / the pictures found are converted into the corresponding int type values in the project, private int parseIcon (String strIcon) {if ("0.gif" .equals (strIcon)) return R.drawable.astat0; if ("1.gif" .equals (strIcon)) return R.drawable.astat1; if ("3.gif" .equals (strIcon)) return R.drawable.a_3 If ("4.gif" .equals (strIcon)) return R.drawable.aqi4; if ("5.gif" .equals (strIcon)) return R.drawable.aqun5; if ("6.gif" .equals (strIcon)) return R.drawable.aqi6; if ("7.gif" .equals (strIcon)) return R.drawable.a_7 If ("8.gif" .equals (strIcon)) return R.drawable.aqi8; if ("9.gif" .equals (strIcon)) return R.drawable.aqiang9; if ("10.gif" .equals (strIcon)) return R.drawable.astat10; if ("11.gif" .equals (strIcon)) return R.drawable.a_11 If ("12.gif" .equals (strIcon)) return R.drawable.aqiao 12; if ("13.gif" .equals (strIcon)) return R.drawable.aqiao 13; if ("14.gif" .equals (strIcon)) return R.drawable.aqiao 14; if ("15.gif" .equals (strIcon)) return R.drawable.a_15 If ("16.gif" .equals (strIcon)) return R.drawable.aqiu 16; if ("17.gif" .equals (strIcon)) return R.drawable.aqiao 17; if ("18.gif" .equals (strIcon)) return R.drawable.aqiao 18; if ("19.gif" .equals (strIcon)) return R.drawable.a_19 If ("20.gif" .equals (strIcon)) return R.drawable.aqiao 20; if ("21.gif" .equals (strIcon)) return R.drawable.aqum21; if ("22.gif" .equals (strIcon)) return R.drawable.aqiao 22; if ("23.gif" .equals (strIcon)) return R.drawable.a_23 If ("24.gif" .equals (strIcon)) return R.drawable.aqiu 24; if ("25.gif" .equals (strIcon)) return R.drawable.aqiu 25; if ("26.gif" .equals (strIcon)) return R.drawable.aqiao 26; if ("27.gif" .equals (strIcon)) return R.drawable.a_27 If ("28.gif" .equals (strIcon)) return R.drawable.aqiao 28; if ("29.gif" .equals (strIcon)) return R.drawable.aqiao 29; if ("30.gif" .equals (strIcon)) return R.drawable.aqiu 30; if ("31.gif" .equals (strIcon)) return R.drawable.aqiao 31; return 0 } @ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). Inflate (R.menu.activity_main, menu); return true;}} these are all the contents of the article "how to use soap protocol to access webservice to achieve weather forecasting". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.