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
<%@ 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
<?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省略掉。
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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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下的WebServiceenvelope.dotNet = true;//必须,等价于envelope.bodyOut = request; envelope.setOutputSoapObject(request);//step4 创建HttpTransportSE对象HttpTransportSE ht = new HttpTransportSE(url);//step5 调用WebServiceht.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调用的时候会报出异常
最好是返回值是 一个不是空格的字符串
我也不知道空格为什么异常 只是实验着 有这种情况
更多相关文章
- android滑屏两三事
- Android中调用Rest web服务
- [置顶] android framework Service分析
- 让android定时关机的实现方法
- MediaRecorder流程分析
- 【转】android AsyncTask 为 多任务 多线程 解决方案
- Android(安卓)Context原理分析
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用