Reprinted Android uses ksoap2 to connect to WebService

Source: Internet
Author: User

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: http: // 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: http://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 = "http: // 192.168.0.2: 8080/axis2/services/manager? WSDL ";
Private Static final string namespace = "http://ws.apache.org/axis2 ";
Private Static final string method_name = "getmyfriends ";

This information can be found in WebService.
<Xs: element name = "getmyfriends">
<Xs: complextype>
<Xs: sequence>
<Xs: Element minoccurs = "0" name = "userid" type = "XS: int"/>
<Xs: Element minoccurs = "0" name = "password" nillable = "true" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>

Next, we will start to work on WebService request data, request WebService functions, and encapsulate the two parameters (userid and password) to be used)
Soapobject request = new soapobject (namespace, method_name );
Request. addproperty ("userid", "123456 ");
Request. addproperty ("password", "test ");
Then we will encapsulate the format of the envelope that defines the sent data.
Soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11 );
Send request
Envelope. setoutputsoapobject (request );
Androidhttptransport AHT = new androidhttptransport (URL );
AHT. Call (null, envelope );

Then we can define a soapobject instance to get the data we returned.
Soapobject so = (soapobject) envelope. bodyin;
Here, if the returned data has only one row and only one value, such as the verification function and boolean type, the operation is relatively simple. String getreturn = so. getproperty ("return"); this getreturn is the value you want to obtain.
However, if the returned value is a multi-row value, this method will not work. We must parse the returned information. I have tried to use j2se standard method to obtain it, but an error is reported. The most important thing is that it cannot be used in Android. So here I use a regular expression to parse the data. Let's take a look at the structure of the data returned by him.
Getmyfriendsresponse {return = friendsmessage {
Permitlist = anytype {nickname =; singnature = NULL; userid = 2 ;}; permitlist = anytype {nickname = Jack; singnature = NULL; userid = 1004 ;}; permitlist = anytype {nickname = admin; singnature = leo_admin; userid = 1001 ;};};}
Simply put, he really wants the JSON structure, but it is not...
As for the current solution, I only use regular expressions to parse the regular expression, such as parsing the above content.

// First obtain the number of permitlist (Friends)
String testpattern = "permitlist ";
Int resultlength = result. Length ();
Cresult = cresult. Replace (testpattern ,"");
Int lastlength = (resultlength-cresult. Length ()/testpattern. Length ();

// Obtain the value in each permitlist.
String loginreturn = "", pattern = "nickname = .*?; \ S * singnature = .*?; \ S * userid = .*?; ";
// Dynamically generate a string array to store the information of each friend
String [] getfinalreturn = new string [lastlength];
For (INT I = 0; I <lastlength; I ++ ){
Loginreturn = result. replacefirst ("^. * (" + pattern + "). * $", "$1 ");
Getfinalreturn [I] = loginreturn;
Result = result. Replace (loginreturn ,"");
}
The format stored in this array is nickname = admin; singnature = leo_admin; userid = 1001;
In this way, we can split the two symbols to get the data.

Now, the WebService connection and the data returned by parsing are finished. Although this method looks complicated, at present, the ksoap2 method has not been used to connect to the WebService ..

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.