android下的web service用戶端開發

來源:互聯網
上載者:User

 

最近協助老師開發一個程式,需要調用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.

<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";//調用方法名<br />private static final String NAMESPACE = "http://tempuri.org/";//命名空間<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 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.

   private string GetData()<br />{<br />//指定webservice的命名空間和調用的方法名<br /> SoapObject request = new SoapObject(NAMESPACE, SOAP_METHOD);<br />//產生調用webservice的soap資訊<br /> SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);<br />//webservice伺服器是.net平台<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 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).

 

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 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.

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 />}<br /> 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.