zoukankan      html  css  js  c++  java
  • Android使用ksoap2调用C#webservice体会(转)

    1,在AdroidManifest.xml中加入权限

       <uses-permission android:name="android.permission.INTERNET"/>

    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;
    
    	@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调用的时候会报出异常

    最好是返回值是  一个不是空格的字符串

    我也不知道空格为什么异常 只是实验着 有这种情况

  • 相关阅读:
    Spring.Net + Ibatis.Net + Log4Net 结合搭的一个简单多层开发架子
    驳网上常见的半调子程序员白痴言论
    IT对联大汇总
    ClassBase A = New ClassSon() 全面分析
    C# 反射/映射学习
    C++ 一般类型与变量
    HttpModule是如何工作的(转自大牛)
    SQL Server 中的嵌套事务与@@TranCount(转)
    IT人不要一直做技术(转我也不知道转了几圈了)
    kingcms的双重循环
  • 原文地址:https://www.cnblogs.com/zhwl/p/2114694.html
Copyright © 2011-2022 走看看