WebService application of client Ksoap library call

Source: Internet
Author: User
Tags soap wsdl

KSOAP2 is a class library developed by third parties dedicated to calling WebService on mobile devices. The use of KSOAP2 call WebService can be divided into 6 steps to complete, which mainly uses the
The Soapobject object specifies the method to invoke, and then calls the WebService method through the call method of the Httptransportse object, and finally through the GetResponse
method returns the result. Readers can use the complete example provided in this article to realize the complete process of calling WebService using KSOAP2. At the end of this article we describe how to call the
WebService to prevent UI components from blocking due to service-side failures or other causes.




Call WebService using KSOAP2:

1. Specifies the namespace of the WebService and the method name of the call
Soapobject request = new Soapobject ("Http://service", "getName");

2. Set the parameter value of the calling method
Request.addproperty ("param1", "value1");
Request.addproperty ("param2", "value2");


Ksoap 1.x/2.0 can automatically map four types of soap to Java types
SOAP type Java type
Xsd:int Java.lang.Integer
Xsd:long Java.lang.Long
Xsd:string java.lang.String
Xsd:booleanJava.lang.Boolean


3. Generates SOAP request information that calls the WebService method. This information is described by the Soapserializationenvelope object
Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER11);
Envelope.bodyout = Request;




Constant SOAPENVELOPE.VER10: Corresponds to the SOAP 1.0 specification
Constant SOAPENVELOPE.VER11: Corresponds to the SOAP 1.1 specification
Constant Soapenvelope.ver12: Corresponds to the SOAP 1.2 specification



4. Creates a Httptransportse object.
Httptransportse ht = new Httptransportse ("http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl");


HttpTransport is a powerful helper class to complete the Http-call transport process, which encapsulates everything the network requests, and you don't have to consider serializing messages at all.
We open debug information by setting its Debug property to True.
HttpTransport tx = new HttpTransport (serviceurl);
Ht.debug = true;




5. Use the call method to invoke the WebService method with the following code:
Ht.call (null, envelope);


Since the HttpTransport class actually calls the httpconnection for the network connection, a separate thread must be used to do ksoap work, otherwise it will clog the operation.




6. Use the GetResponse method to get the return result of the WebService method, as follows:
Soapobject Soapobject = (soapobject) envelope.getresponse ();




Import Org.ksoap2.SoapEnvelope;      Import Org.ksoap2.serialization.SoapObject;      Import Org.ksoap2.serialization.SoapSerializationEnvelope;      Import Org.ksoap2.transport.HttpTransportSE;      Import android.app.Activity;      Import Android.os.Bundle;      Import Android.view.View;      Import Android.view.View.OnClickListener;      Import Android.widget.Button;      Import Android.widget.EditText;             Import Android.widget.TextView; public class Main extends Activity implements Onclicklistener {@Override public void OnClick (View              View) {EditText Etproductname = (EditText) Findviewbyid (r.id.etproductname);              TextView Tvresult = (TextView) Findviewbyid (R.id.tvresult); The url,192.168.17.156 of the WSDL document is the ID address of the PC String serviceurl = "Http://192.168.17.156:8080/axis2/services/SearchPro              DUCTSERVICE?WSDL ";             Defines the invocation of the WebService method name String methodName = "GetProduct"; 1th Step: Create the Soapobject object and specify the WebService namespace and the method name called Soapobject request = new Soapobject ("Http://service", Me              Thodname);              2nd step: Set the parameters of the WebService method Request.addproperty ("ProductName", Etproductname.gettext (). toString ()); 3rd Step: Create the Soapserializationenvelope object and specify the version of WebService Soapserializationenvelope envelope = new Soapserializ              Ationenvelope (SOAPENVELOPE.VER11);              Set Bodyout Property envelope.bodyout = Request;                      4th step: Create the Httptransportse object and specify the URL of the WSDL document HTTPTRANSPORTSE HT = new Httptransportse (serviceurl);                  try {//5th step: Call WebService Ht.call (null, envelope);                       if (envelope.getresponse () = null) {//6th step: Use the GetResponse method to get the return result of the WebService method                      Soapobject Soapobject = (soapobject) envelope.getresponse (); Get Pro with the GetProperty methodDuct property value of the object String result = "Product Name:" + soapobject.getproperty ("name") + "\ n";                      Result + = "Number of products:" + soapobject.getproperty ("ProductNumber") + "\ n";                      Result + = "Product Price:" + soapobject.getproperty ("prices");                         Tvresult.settext (result);                  } else {Tvresult.settext ("no this product."); }} catch (Exception e) {}} @Override Pub              LIC void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);              Setcontentview (R.layout.main);              Button btnsearch = (button) Findviewbyid (R.id.btnsearch);          Btnsearch.setonclicklistener (this);   }      }






Asynchronous processing results
  Import Org.ksoap2.SoapEnvelope;      Import Org.ksoap2.serialization.SoapObject;      Import Org.ksoap2.serialization.SoapSerializationEnvelope;      Import Org.ksoap2.transport.HttpTransportSE;      Import android.app.Activity;      Import Android.os.AsyncTask;      Import Android.os.Bundle;      Import Android.view.View;      Import Android.view.View.OnClickListener;      Import Android.widget.Button;      Import Android.widget.EditText;             Import Android.widget.TextView;          public class Main extends Activity implements Onclicklistener {private EditText etproductname;                 Private TextView Tvresult;              Class Wsasynctask extends Asynctask {String result = "";                  @Override protected Object Doinbackground (Object ... params) {try           {String serviceurl = "http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl";           String methodName = "GetProduct";                      Soapobject request = new Soapobject ("Http://service", methodName);                      Request.addproperty ("ProductName", Etproductname.gettext (). toString ());                      Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER11);                      Envelope.bodyout = Request;                             Httptransportse ht = new Httptransportse (serviceurl);                      Ht.call (null, envelope); if (envelope.getresponse () = null) {Soapobject Soapobject = (soapobject) E                          Nvelope.getresponse ();                          result = "Product Name:" + soapobject.getproperty ("name") + "\ n";                          Result + = "Number of products:" + soapobject.getproperty ("ProductNumber") + "\ n"; Result + = "Product Price:" + SOAPOBJECT.getproperty ("Price");                      } else {result = "There is no such product."; }} catch (Exception e) {result = "Call WebService Error                  .";                  }                      You must use the Post method to update the UI component Tvresult.post (new Runnable () {@Override                             public void Run () {tvresult.settext (result);                  }                  });              return null;                }} @Override public void OnClick (view view) {//asynchronously executes the task calling WebService          New Wsasynctask (). Execute (); } @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (saved              Instancestate); Setcontentview (r.layout.main);              Button btnsearch = (button) Findviewbyid (R.id.btnsearch);              Btnsearch.setonclicklistener (this);              Etproductname = (EditText) Findviewbyid (r.id.etproductname);                 Tvresult = (TextView) Findviewbyid (R.id.tvresult);   }      }









WebService application of client Ksoap library call

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.