The WebServices of Android development

Source: Internet
Author: User
Tags http post soap wsdl

A: webservices introduction

The introduction of WebServices is very detailed in the world of information. WebService is a network application program, the basic WebService platform is xml+http.

  • The HTTP protocol is the most commonly used Internet protocol.
  • XML provides a language that can be used between different platforms and programming languages.
Elements of the WEB services platform:
  • SOAP (Simple Object Access Protocol): XML-based, language-independent exchange of information between applications via HTTP.
  • UDDI (general description, Discovery, and Consolidation): A directory of network Service interfaces, described by WSDL, through which enterprises can register and search for WEB services.
  • WSDL (Web Services Description Language): XML-based language used to describe WEB services and how to access them.

Use of WebService

Place a Web service on a site by using forms and HTTP POST. Send messages between applications through XML.

By using WebService, we are able to invoke methods on the remote server just like local methods. There is no need to worry about what the server is written on and what platform it is based on.

Second: WebService call under Android platform

Calling WebService in Android requires importing a third-party jar package, ksoap2-android, right-hand project "Build Path"-"Config Build Path ...", select the Jar package check box, OK.

Three: Application

This application is mainly to use Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx's number attribution to WebService to achieve the attribution of the query:

<?XML version= "1.0" encoding= "Utf-8"?>  <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:paddingtop= "5dip"Android:paddingleft= "5dip"Android:paddingright= "5dip"      >      <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Please enter the phone number to be queried:"      />      <EditTextAndroid:id= "@+id/phone_sec"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Textphonetic"Android:singleline= "true"Android:hint= "For example: 1398547"      />      <ButtonAndroid:id= "@+id/query_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Right"Android:text= "Query"      />      <TextViewAndroid:id= "@+id/result_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal|center_vertical"      />  </LinearLayout>

CODE:

Here the face of simple edittext input check does not do, in addition here need to figure out namespace, methodname,asmx,action variable, in http://webservice.webxml.com.cn/ Webservices/mobilecodews.asmx? In the WSDL description document, it is clear that the ASMX is the WSDL document to remove the following parameters, in practice, found on the band can also request to the data. The action is the namespace plus the method name.

   /*** Mobile phone number section attribution to search * *@paramphonesec Mobile phone number segment*/       Public voidGetremoteinfo (String phonesec) {//name SpaceString nameSpace = "http://WebXml.com.cn/"; //called method nameString methodName = "Getmobilecodeinfo"; //asmxString asmx = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; //SOAP ActionString soapaction = "Http://WebXml.com.cn/getMobileCodeInfo"; //specifies the namespace of the WebService and the method name of the callSoapobject sobj =NewSoapobject (NameSpace, methodName); //set the two parameters that need to be passed in to call the WebService interface Mobilecode, UserIdSobj.addproperty ("Mobilecode", phonesec); Sobj.addproperty ("UserId", "" "); //generates SOAP request information that calls the WebService method and specifies the version of SoapSoapserializationenvelope envelope =NewSoapserializationenvelope (SOAPENVELOPE.VER10); Envelope.bodyout=Sobj; //sets whether to invoke the dotnet developed WebServiceEnvelope.dotnet =true; //equivalent to envelope.bodyout = RPC; Envelope.setoutputsoapobject (sobj); Httptransportse Transport=NewHttptransportse (ASMX); Try {              //Call WebServiceTransport.call (soapaction, envelope); } Catch(Exception e) {e.printstacktrace (); }            //get the returned dataSoapobject object = (soapobject) envelope.bodyin/ *. GetResponse ()* /; //gets the returned resultString result = Object.getproperty ("Getmobilecodeinforesult"/  *0*  /). toString (); //Displays the results returned by WebService in TextViewResultview.settext (Result); }

There are two ways to see that you can get objects and get results. Are the methods provided in SAOP.

If there are multiple values in the retrieved data, you can get the number of results by Result.getpropertycount ()

At some point, some inexplicable exceptions may be reported, try to modify the SOPA version, or set the action to null (some do not need action)

Permissions:

Last thing: Don't forget to add permission

<android:name= "Android.permission.INTERNET"/>

The WebServices of Android development

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.