Android Call WebService with SoapHeader authentication

Source: Internet
Author: User
Tags addchild

A recent project called the WebService interface, need to verify the SoapHeader, the resolution is now recorded as follows: (the online data source too much, do not make references, the original author as seen, if necessary to add please notify)

1, first look at the interface

Post/webserver/validewebservice.asmx http/1.1Host:ip Address Content-type:text/xml; charset=utf-8Content-length:lengthsoapaction:"HTTP//namespace/login"<?xml version="1.0"encoding="Utf-8"? ><soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"Xmlns:xsd="Http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <mysoapheader xmlns="/HTTP//namespace/"> <ProjectID>string</ProjectID> </MySoapHeader> </soap:Header> <soap:Body> <login xmlns="/HTTP//namespace/"> <loginName>string</loginName> <passowrd>string</passowrd> </Login> </soap:Body></soap:Envelope>

Verification needs to verify the header and body two parts, need to introduce a third-party jar package, Ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar. Here is my verification of the use of methods, there are many online, but can not be verified, "take" to modify, make a record, for later review, but also for the convenience of everyone to see.

declare the following first;//name Space    Private Static FinalString NAMESPACE = "http//Namespace/"; //Service Address    Private StaticString URL = "Http://IP address or domain name/webserver/validewebservice.asmx"; //method name to invoke    Private Static FinalString method_name = "Login"; //here is the namespace + method name    Private StaticString soap_action = "/http/namespace/login"; PrivateSoapobject detail;

Since more than 2.3 can not directly access the network in the main thread, so the need to open a sub-thread, where I click on the button to log in, so write in the OnClick () method:

NewThread () {@Override Public voidrun () {//TODO auto-generated Method Stub                    Super. Run (); Try{soapobject RPC=NewSoapobject (NAMESPACE, method_name); //here are 2 PropertyInfo, which are the parameters required by the login method, and the ASMX code is posted underneath the codePropertyInfo pi =NewPropertyInfo (); Pi.setname ("LoginName");                        Pi.setvalue (CARDNUMSTR);                        Rpc.addproperty (PI); Pi=NewPropertyInfo (); Pi.setname ("PASSOWRD");                        Pi.setvalue (PASSWORDSTR);                        Rpc.addproperty (PI); //SoapHeader is here .Element[] Header =NewElement[1]; header[0] =NewElement (). createelement (NAMESPACE, "Mysoapheader"); Element username=NewElement (). createelement (NAMESPACE, "ProjectID"); Username.addchild (Node.text,"Here is the value"); header[0].addchild (node.element, username); Soapserializationenvelope Envelope=NewSoapserializationenvelope (SOAPENVELOPE.VER11); Envelope.headerout=header; Envelope.bodyout=RPC; Envelope.dotnet=true;                        Envelope.setoutputsoapobject (RPC); Httptransportse HT=NewHttptransportse (URL);                        Ht.call (soap_action, envelope); SoapobjectDetail =(Soapobject) envelope.getresponse (); System.out.println ("Returned results" +detail.tostring ()); }Catch(Exception e) {System.out.println ("Error message:" +e.getmessage ()); } Message msg=Handler.obtainmessage (); Msg.obj=detail;                Handler.sendmessage (msg); }}.start ();

The above cardnumstr and passwordstr are the values I get from the text input box. Access to the network from the interface through authentication and then get the return value, the returned data processing on it.

With Soapobject, the detail returned is null.

Private New Handler () {        publicvoid  handlemessage (Message msg) {            //  Do your UI processing   here         };    };

Android Call WebService with SoapHeader authentication

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.