Use Ksoap2 to call Web Service to add SoapHeader and ksoap2soapheader

Source: Internet
Author: User

Use Ksoap2 to call Web Service to add SoapHeader and ksoap2soapheader

For this question, if you use Baidu all the code in the previous article, it is hard to find the complete method on google. Here we will talk about all the code and share it with you.

The first is the background code written by. NET.

/// <Summary> /// SoapHeader /// </summary> public class OwnSoapHeader: SoapHeader {public string UserName {get; set;} public string Password {get; set ;}//< summary> // summary of WebService1 /// </summary> [WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] [System. componentModel. toolboxItem (false)] // to allow ASP. net ajax calls this Web service from the script, Uncomment the following lines. // [System. web. script. services. scriptService] public class WebService1: System. web. services. webService {public OwnSoapHeader; [WebMethod] [SoapHeader ("OwnSoapHeader")] public string Login () {if (OwnSoapHeader! = Null) {return OwnSoapHeader. UserName + "," + OwnSoapHeader. Password;} return "not logged on ";}}

The next step is to browse and view my interface in the browser, which contains important information, including the SoapHeader name and parameters.

Then, Ksoap2 is used to call my interface.

Private void DoLogin () {new Thread () {@ Override public void run () {Looper. prepare (); // TODO Auto-generated method stub String sNameSpace = "http://tempuri.org/"; String sMethodName = "Login"; String sActionString = "http://tempuri.org/Login"; String sURL = "http: // 192.168.1.101: 8088/WebService1.asmx "; SoapObject rpc = new SoapObject (sNameSpace, sMethodName); // SoapHeader Element [] header = new Element [1] is added here;
// The OwnSoapHeader and the red flag have the same name. header [0] = new Element (). createElement (sNameSpace, "OwnSoapHeader ");
// The UserName is marked in red with the same name as Element username = new Element (). createElement (sNameSpace, "UserName"); username. addChild (Node. TEXT, "admin"); header [0]. addChild (Node. ELEMENT, username );
// The Password marked in red has the same name as Element pass = new Element (). createElement (sNameSpace, "Password"); pass. addChild (Node. TEXT, "123"); header [0]. addChild (Node. ELEMENT, pass); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER12); envelope. headerOut = header; envelope. bodyOut = rpc; envelope. dotNet = true; envelope. setOutputSoapObject (rpc); HttpTransportSE ht = new HttpTransportS E (sURL); SoapObject soapObject = null; try {ht. call (sActionString, envelope); soapObject = (SoapObject) envelope. bodyIn;} catch (IOException ex) {// TODO Auto-generated catch block ex. printStackTrace ();} catch (XmlPullParserException ex) {// TODO Auto-generated catch block ex. printStackTrace ();} Bundle result = new Bundle (); if (soapObject! = Null) {result. putString ("result", soapObject. toString ();} else {result. putString ("result", sURL);} Message msg = new Message (); msg. setData (result); msg. what = 1; handler. handleMessage (msg); logoff. loop ();}}. start ();} Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {// TODO Auto-generated method stub if (msg. what = 1) {Bundle result = msg. getData (); String text = result. getString ("result"); Toast. makeText (MainActivity. this, text, Toast. LENGTH_LONG ). show ();}}};

Now all the user data has been OK and can receive the user data sent back from the server.

 


Android uses KSOAP2 to call WebService

Android uses ksoap2 to connect to webservice (16:36:25). Reprinted Tag: androidksoap2webserviceit category: Android
Using J2SE's ksoap2 standard, I also made a fake version of android to connect to webservice. Because of the soap encapsulation relationship, android applications cannot correctly obtain data after receiving data according to J2SE standards.

Before using it, we need to guide the two jar files into the buildpath of the project.

Both jar packages can be found and downloaded online. After guidance, make preparations. Find out the address of the published webservice and the encapsulation method. For example:

Webservice Interface: 192.168.0.2: 8080/axis2/services/Manager? Wsdl (by the way, in android, localhost cannot be written, and the current network IP address of the PC must be clearly written)
Webservice encapsulation: ws.apache.org/axis2

After learning all about it, it means you are ready.
The following describes how android obtains webservice encapsulated data ..

Introduce ksoap2 to encapsulate classes
Import org. ksoap2.SoapEnvelope;
Import org. ksoap2.serialization. SoapObject;
Import org. ksoap2.serialization. SoapSerializationEnvelope;
Import org. ksoap2.transport. AndroidHttpTransport;

Define the webservice interface address and resolution method in the class and define the functions in the webservice to be called
Private static final String URL = "192.168.0.2: 8080/axis2/services/Manager? Wsdl ";
Private static final String NAMESPACE = "ws.apache.org/axis2 ";
Private static final String METHOD_NAME = "GetMyFriends ";

This information can be found in webservice.
<Xs: element name = "Ge ...... remaining full text>
 
Android uses ksoap2 to call net webservice

I have used some packages, but they are incorrect. It's best to go to the official website.

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.