recently in the study Android , with the popularity of mobile devices, when the software embarked on the commercialization of the road. In order to compete for the market, definitely need to support Android , so started to touch android, just understand the fur just fine, because we want to be a manager. Understand dot Android. Easy to manage.
Android It's also easy to learn, packaged better, and a control that looks like it's back VB the bright.
The following is a demonstration example of how to invoke a Web Service on an Android platform.
We use the Internet ready-made Webservice. a Web service for querying the destination of a mobile phone number whose WSDL is http://webservice.webxml.com.cn/ WEBSERVICES/MOBILECODEWS.ASMX?WSDL.
1) New Androidproject. Introduction of the Ksoap2-android class library downloaded above
Calling WebService on the Android platform is dependent on the third-party class library KSOAP2. It is a SOAP webserviceclient development package that is used primarily for resource-constrained Java environments such as applets or J2ME applications (CLDC/CDC/MIDP).
and We do not use KSOAP2 directly on the Android platform. Instead, use Ksoap2android. KSoap2 Android is an efficient, lightweight soap development package on the Android platform
2) write the layout file Res/layout/main.xml
<?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_pare NT "android:paddingtop=" 5dip "android:paddingleft=" 5dip "android:paddingright=" 5dip "> <tex TView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "hand Machine number (segment): "/> <edittext android:id=" @+id/phone_sec "android:layout_width=" Fill_parent "and roid:layout_height= "Wrap_content" android:inputtype= "Textphonetic" android:singleline= "true" an Droid:hint= "such as: 1398547"/> <button android:id= "@+id/query_btn" android:layout_width= "wrap_content "Android:layout_height=" wrap_content "android:layout_gravity=" right "android:text=" query "/& Gt <textview android:id="@+id/result_text" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:layout_gravity= "Center_horizontal|center_vertical"/> </LinearLayout>
3) Writing Mainactivity class
/** * Android Platform call WebService (mobile number Attribution query) * */public class Mainactivity extends Activity {private EditText phones Ecedittext; Private TextView Resultview; Private Button Querybutton; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Forces operations on the UI thread Strictmode.setthreadpolicy (NewStrictMode.ThreadPolicy.Builder (). Detectdiskreads (). Detectdis Kwrites (). Detectnetwork (). Penaltylog (). build ()); Strictmode.setvmpolicy (NewStrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects (). Penaltylog (). Penaltydeath (). Build ()); Phonesecedittext = (EditText) Findviewbyid (R.ID.PHONE_SEC); Resultview = (TextView) Findviewbyid (R.id.result_text); Querybutton = (Button) Findviewbyid (R.ID.QUERY_BTN); Querybutton.setonclicklistener (New Onclicklistener () {@Override publicvoid OnClick (View v) {//Mobile number (segment) String phonesec = Phonesecedittext.gettext (). ToString ( ). Trim (); Simply infer whether the mobile number (segment) entered by the user is legal if ("". Equals (phonesec) | | Phonesec.length () < 7) {//Give error Phonesecedittext.seterror ("The mobile number (segment) you entered is incorrect. "); Phonesecedittext.requestfocus (); The TextView that displays the results of the query is emptied Resultview.settext (""); Return }//Query mobile number (segment) Information getremoteinfo (PHONESEC); } }); }/** * Mobile number Segment Attribution Query * * @param phonesec Mobile phone number segment */public void Getremoteinfo (String phonesec) {//namespace String nameSpace = "http://WebXml.com.cn/"; The method name called String methodName = "Getmobilecodeinfo"; EndPoint String EndPoint = "Http://webservice.webxml.com.cn/WebServices/MobiLecodews.asmx "; SOAP Action String soapaction = "Http://WebXml.com.cn/getMobileCodeInfo"; Specifies the namespace of the WebService and the method name called Soapobject RPC = new Soapobject (NameSpace, methodName); Set the two Mobilecode, UserId rpc.addproperty ("Mobilecode", phonesec) that need to be passed in to call the WebService interface; Rpc.addproperty ("UserId", ""); Generates a SOAP request message that calls the WebService method and specifies the version number of soap Soapserializationenvelope envelope = new Soapserializationenvelope (soape Nvelope. VER10); Envelope.bodyout = RPC; Sets whether to invoke the DotNet developed webservice envelope.dotnet = true; Equivalent to envelope.bodyout = RPC; Envelope.setoutputsoapobject (RPC); Httptransportse transport = new Httptransportse (endPoint); try {//Call WebService Transport.call (SOAPAction, envelope); } catch (Exception e) {e.printstacktrace (); }//Gets the returned data soapobject object = (soApobject) Envelope.bodyin; Gets the result returned by String result = Object.getproperty (0). toString (); The results returned by WebService are displayed in TextView resultview.settext (result); } }
watch out.1:NamespAce,MethodName,EndPointand theSOAP Action information. are able toWSDLobtained in the.
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Note 2 : call webservice The interface method needs to pass in the number of parameters. The name of the reference is to and wsdl
( some sources on the web say that when multiple parameters are required, the order of only multiple parameters is consistent with the order in which the parameters in the WSDL appear.) The name does not need to be consistent with the WSDL, but the actual test finds that it is not feasible in most cases. )
Note Point 3: The results returned after calling WebService In this example are as follows:
<?
XML version= "1.0" encoding= "Utf-8"?
>
<stringxmlns="http://WebXml.com.cn/">1398547: GuizhouGuiyangGuizhou Mobile Qian mid -stream card</string>
This is what is clearly returned in the XML format, why don't we need to parse the XML to get what we need? In fact:
// Get the returned data
Soapobject object = (soapobject) Envelope.bodyin;
KSOAP2 can convert the returned XML to a Soapobject object. Then we can get the data we need by manipulating the object.
watch out.4:in this example, only one value is returned. But someWebServicewill return more than one value how to get it? The Fetch method is exactly the same as in this example, and it is only necessary to note that the assumption is that multiple values are returned. Through section -Codeobject.getproperty (0);the resulting may still be aSoapobject.
Call the GetProperty () method constantly, and always get all the results you want.
4) Configure permissions to join the access network in Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/ Android " package=" com.liufeng.ws.activity " android:versioncode=" 1 " android:versionname=" 1.0 "> <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android: Name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application> <uses-sdk android:minsdkversion = "4"/> <!--access to network permissions-- <uses-permission android:name= "Android.permission.INTERNET"/> </manifest>
5) Execution Results
Source code Download
http://download.csdn.net/detail/tcl_6666/7365311
Android platform invoke Web Service: Demo sample