Android使用ksoap2调用C#webservice体会(转)
1,在AdroidManifest.xml中加入权限
2,导入ksoap2包
在java中使用的PC版WebService客户端库非常丰富,例如,Axis2、CXF等,但这些开发包对于android来说过于庞大,也未必很容易移植到android上。适合手机的WebService客户端SDK也有一些。本例使用了比较常用的KSOAP2。读者可以从如下的地址下载Android版的KSOAP2。
http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2
将下载下来的包引用到android项目后就可以使用了,在引用jar包后可能会抛出警告"warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)”,在网上搜索了一下,这可能是因为版本的问题,但是不影响使用。好了废话不多说,直接上代码。
服务器端的webservice文件Demo.asmx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ WebService Language= "C#" Class= "Demo" %> using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/" )] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class Demo : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World" ; } [WebMethod] public string Add( int x, int y) { int z = x + y; return z.ToString(); } } |
下面就是我测试用的手机端的代码了,首先来看我们的xml布局文件 demo.xml
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <TextView android:id= "@+id/tv" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:padding= "10dip" > </TextView> </LinearLayout> |
Demo.java,在下面的代码中,如果你的WebService方法没有参数,可以把step2省略掉。
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 57 58 59 60 61 62 63 64 | package com.studio.basf.android; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Demo extends Activity { private String NameSpace = "http://tempuri.org/" ; private String MethodName = "Add" ; private String url = "http://192.168.1.93/services/Demo.asmx" ; private String soapAction = NameSpace + MethodName; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.demo); tv = (TextView) findViewById(R.id.tv); tv.setText(ws()); } public String ws() { String result = "" ; try { //step1 指定WebService的命名空间和调用的方法名 SoapObject request = new SoapObject(NameSpace, MethodName); //step2 设置调用方法的参数值,这里的参数名称最好和WebService一致 request.addProperty( "x" , 5 ); request.addProperty( "y" , 6 ); //step3 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); //设置是否调用的是dotNet下的WebService envelope.dotNet = true ; //必须,等价于envelope.bodyOut = request; envelope.setOutputSoapObject(request); //step4 创建HttpTransportSE对象 HttpTransportSE ht = new HttpTransportSE(url); //step5 调用WebService ht.call(soapAction, envelope); //step6 使用getResponse方法获得WebService方法的返回结果 if (envelope.getResponse()!= null ){ SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); result = response.toString(); } } catch (Exception e) { result = e.getMessage(); } return result; } } |
运行结果如下
2011年8月18日14:24:18 补充:
如果c#编写的webservices 的返回值是空格 android调用的时候会报出异常
最好是返回值是 一个不是空格的字符串
我也不知道空格为什么异常 只是实验着 有这种情况
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】