WebService example for Android weather forecast call

Source: Internet
Author: User

The android SDK does not provide a library to call WebService. Therefore, you must use a third-party SDK to call WebService. Ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar I use

When importing an incorrect package, refer to my previous blog

:

Code details:

Package CN. BZU. weatherforecast. soap; import Java. io. ioexception; import Org. ksoap2.soapenvelope; import Org. ksoap2.serialization. soapobject; import Org. ksoap2.serialization. soapserializationenvelope; import Org. ksoap2.transport. httptransportse; import Org. xmlpull. v1.xmlpullparserexception; public class soaputil {public static object dotransport (final string wsdlurl, final string webmethod, string cityname) {String namespace = "http://WebXml.com.cn/"; // WebService namespace soapobject = new soapobject (namespace, webmethod); soapobject. addproperty ("thecityname", cityname); // you can specify the value of the parameter system. out. println (cityname); // generate the SOAP request information soapserializationenvelope = new soapserializationenvelope (soapenvelope. ver11); soapserializationenvelope. bodyout = soapobject; soapserial Izationenvelope. DOTNET = true; soapserializationenvelope. setoutputsoapobject (soapobject); // create an httptransportsse object. Httptransportse = new httptransportse (wsdlurl); httptransportse. DEBUG = true; string soap_action = "http://WebXml.com.cn/" + webmethod; system. out. println (soap_action); try {// call the WebService method httptransportse using the call method. call (soap_action, soapserializationenvelope); // obtain the returned result of the WebService method if (soapserializationenvelope. getresponse ()! = NULL) {soapobject result = (soapobject) soapserializationenvelope. getresponse (); system. out. println ("Result:" + result); return result ;}} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (xmlpullparserexception e) {// todo auto-generated catch blocke. printstacktrace ();} return NULL ;}}
Package CN. BZU. weatherforecast; import Org. ksoap2.serialization. soapobject; import CN. BZU. weatherforecast. soap. soaputil; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. edittext; import android. widget. textview; public class mainactivity extends activity {private edittext citytext; private button serach; private textview resultview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); citytext = (edittext) findviewbyid (R. id. city); serach = (button) findviewbyid (R. id. search); resultview = (textview) findviewbyid (R. id. result); serach. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {string cityname = citytext. gettext (). tostring (); string wsdlurl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; string method = "getweatherbycityname"; // call method of WebService soapobject result = (soapobject) soaputil. dotransport (wsdlurl, method, cityname); system. out. println ("Result:" + result); resultview. settext (result. getproperty (10 ). tostring () ;}}) ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

Layout file:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/city" /><EditText android:id="@+id/city" android:layout_width="fill_parent"android:layout_height="wrap_content" /><Button android:id="@+id/search" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="@string/search" /><TextView android:id="@+id/result" android:layout_width="fill_parent"android:layout_height="wrap_content" /></LinearLayout>

Error: Java. Lang. nullpointerexception

Double-click error. The error code is resultview. settext (result. getproperty (10). tostring ());

Analysis: The resultview or result is a null pointer, And the resultview is a lookup component with a low possibility of error, so it is likely that the result is a null pointer. If the result is null, check the code: soapobject result = (soapobject) soaputil. dotransport (wsdlurl, method, cityname); check whether wsdlurl can be opened, whether method is called correctly, and finally consider soaputil. dotransport method is correct
Finally, do not forget to set the permission <uses-Permission Android: Name = "android. Permission. Internet"> </uses-Permission> In androidmanifest. xml. Otherwise, a null pointer may be reported.

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.