最近协助老师开发一个程序,需要调用webservice服务,找到了以下资料,与大家分享。
While working on a new mobile application for one of my clients, I had to make calls out to web services to retrieve(取回) data. Using the Android framework to make these calls was very cumbersome(难处理的).
After doing a little Googling I found a nifty library to import into my application that made web service calls for the Droid simplistic. KSOAP2(用到的第三方类库), a free library to download, made calling SOAP web services a breeze(轻而易举). KSOAP2 can be downloaded by 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(原始) data types. The second example will use 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 to allow your Android application to access websites and services. Start by adding the following line to your manifest file.
Next we are going to create an activity Primitive.java. Inside the Primitive activity you will need to import the following classes.
Next we are going to add some local variables to the activity.
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 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 a function in our activity called GetData.
One thing to 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 a 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).
OK, now for the 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 except 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 the activity. This time we are going to return a generic object. This object will contain two fields, FirstName and LastName.