android下的web service客户端开发

 

最近协助老师开发一个程序,需要调用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 linkhttp://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.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值