Recently, I helped the teacher develop a program and needed to call the WebService. I found the following information and shared it with you.
While working on a new mobile application
For one of my clients, I had to make callout to web services to retrieve (retrieve) data. Using the android framework
Make these callwas very cumbersome (hard to handle ).
After doing a little googling I found
Nity library to import into my application that made web service CILS for
Droid simplistic. ksoap2 (used third-party class library), a free library to download, made calling soap Web Services
Breeze (easy to use). ksoap2 can be downloaded
Clicking the following link (http://sourceforge.net/projects/ksoap2 ).
Once you have
Ksoap2 downloaded I'll go through two examples of using ksoap2. the first
Example will use primitive (original) data types. The second example will use complex (complex) data types and
Objects from the WSDL.
For the primitive example we are going
To call a fictions Web Service http://www.hypersonictechnologies.com/example.asmx,
And call the helloworld web method. This method will return back "Hello
World ".
For both examples we need to modify our
Manifest file to allow the Internet permission.
The Internet permission is a must
Allow your Android Application to access websites and services. Start
Adding the following line to your manifest file.
<Uses-Permission Android: Name = "android. Permission. Internet"/>
Next we are going to create an activity primitive. java. Inside the primitive activity you will
Need to import the following classes.
Import Org. ksoap2.soapenvelope; <br/> Import Org. ksoap2.serialization. soapobject; <br/> Import Org. ksoap2.serialization. soapprimitive; <br/> Import Org. ksoap2.serialization. soapserializationenvelope; <br/> Import Org. ksoap2.transport. androidhttptransport;
Next we are going to add some local
Variables to the activity.
Private Static final string soap_action = "http://tempuri.org/HelloWorld"; // soap_method + namespace <br/> Private Static final string soap_method = "helloworld "; // call method name <br/> Private Static final string namespace = "http://tempuri.org/"; // namespace <br/> Private Static final string url = "http://www.hypersonictechnologies.com/Example.asmx "; <br/>
Soap_action is the namespace of our web
Service and the name of the method we are going to call.
Soap_method is the name of the web method we are
Going to call.
Namespace is the namespace of our web service.
This can be found in the attributes of your web service. By default if you are
Using Microsoft Visual Studio http://tempuri.org/will be the default.
URL is the address to the Windows Web service.
Now that we have our variables setup, we
Can now make a call to helloworld and receive data. I'm going tocreate
Function in our activity called getdata.
Private string getdata () <br/>{< br/> // specify the namespace of the WebService and the name of the method called <br/> soapobject request = new soapobject (namespace, soap_method); <br/> // generate the soap information that calls WebService. <br/> soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); <br/> // the WebService server is.. NET platform <br/> envelope. DOTNET = true; <br/> envelope. setoutputsoapobject (request); <br/> androidhttptransport AHT = new androidhttptransport (URL); <br/> @ suppresswarnings ("UNUSED") <br/> Object result; <br/> try <br/> {<br/> AHT. call (soap_action, envelope); <br/> result = (object) envelope. getresponse (); <br/>}catch (exception ex) {<br/> result = NULL; <br/>}< br/> return result; <br/>}< br/>
One thing
Notice in the Code is the line (envelope. DOTNET = true;). You'll want to make
Sure you have this line when calling ASP. NET web services. Without this line your Web Service
Will return errors, and like most errors they aren't very descriptive.
OK calling
Primitive Web service is made easier now with ksoap2, but what if you want to pass in
Parameters to the Web service? Well for that we just add a few extra lines (1 for each
Parameter). We'll use the existing function getdata, but we will make it so we
Pass in two parameters (string firstname and string lastname ).
Private string getdata (string firstname, string lastname) <br/>{< br/> soapobject request = new soapobject (namespace, soap_method); <br/> request. addproperty ("firstname", firstname); <br/> request. addproperty ("lastname", lastname); <br/>... <br/>}< br/>
OK, now for
Fun part. I don't want to send back a primative from my web service, instead I
Want to send back a list, or any other object I have defined. For this example
We are going to create an activity named custom. java. In this activity we will
Setup the Web Service exactly the same as primative. Java does t we do not need
To import org. ksoap2.serialization. soapprimitive.
Since the rest
Of the Code is the same I'm going to skip down to the getdata method of
Activity. This time we are going to return a generic object. this object will contain in two
Fields, firstname and lastname.
Private void getdata (string accountnumber) <br/>{< br/> soapobject request = new soapobject (namespace, soap_method); <br/> request. addproperty ("Account", accountnumber); <br/> soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); <br/> envelope. DOTNET = true; <br/> envelope. setoutputsoapobject (request); <br/> androidhttptransport AHT = new androidhttptransport (URL); <br/> try <br/>{< br/> AHT. call (soap_action, envelope); <br/> soapobject resultsrequestsoap = (soapobject) envelope. bodyin; <br/> soapobject player = (soapobject) resultsrequestsoap. getproperty ("getaccountresult"); <br/> string fname = player. getproperty ("firstname "). tostring (); <br/> string lname = player. getproperty ("lastname "). tostring (); <br/>}catch (exception ex) {<br/> result = NULL; <br/>}< br/>