Android 调用WCF实例
1. 构建服务端程序
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 |
|
2. 构建IIS网站宿主
YourService.svc
<%@ServiceHost Debug="true" Service="YourNameSpace.YourService"%>
Web.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
3. 寄宿服务
把网站发布到web服务器, 指定网站虚拟目录指向该目录
如果你能够访问http://你的IP:端口/虚拟目录/服务.svc
那么,恭喜你,你的服务端成功了!
4. 使用ksoap2调用WCF
去ksoap2官网
http://code.google.com/p/ksoap2-android/ 下载最新jar
5. 在Eclipse中新建一个Java项目,测试你的服务
新建一个接口, 用于专门读取WCF返回的SoapObject对象
ISoapService
1 2 3 4 5 6 7 8 9 |
|
HelloService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
测试程序
1 2 3 4 5 6 7 8 9 10 11 12 |
|
经过测试成功
运行结果:
Hello Master HaKu
6. Android客户端测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
7. 最后运行结果
安卓(Android)开发:利用ksoap2调用webservice服务,并自动生成客户端代理类代码
安卓调用Webservice和Java稍有不同,利用的是ksoap2这个jar包。之前这个jar包是发布在googlecode上面的目前项目已经移动到了github.io,我这里贴上的github官方网站,我也不知道这个github.io和github.com是不是一回事。我们可以在以下页面看到项目的总览:http://simpligility.github.io/ksoap2-android/index.html。
1.下载ksoap2jar包
ksoap2项目的源码在这里,有兴趣的可以弄下来研究哦:
2.在Android Studio中进行配置
这一步简单,先放到lib文件夹下,然后再lib上点击右键,选择ADD AS LIB就可以了哦
3.利用网上的服务,自动生成ksoap2可用的webservice的客户端代理类
打开http://www.wsdl2code.com/pages/Home.aspx页面,在页面的右边填入你的webService的访问地址,然后选择生成的方式,我选的是Android Using kSoap2.如果你的webservice还没有发布,也可以直接上传其wsdl文件。
点击submit,此时要求登录,如果没有账号就注册一个,然后登陆,稍等一会这个工具就会为我们自动生成Webservice客户端代理类的代码了,点击下载
当然,自动生成的没有与jar运行环境啊什么的,可能使用的时候有些问题,至少包命名就得改成你自己的,所以,我们再简单的修改一下这些代码就可以直接使用了,省去了我们手动写客户端代理类的麻烦,是不是很方便啊。
原文地址:https://www.cnblogs.com/Alex80/p/11111891.html