• Android调用C#的WebService


    Android调用C#写的WebService

    学习自:

    http://www.cnblogs.com/kissazi2/p/3406662.html

    运行环境

    Win10

    VS 2015

    Android Studio 2.2.3

    KSOAP2的Jar包

    Overview

    众所周知,符合W3C标准的,HTTP协议、SOAP 协议等,是没有语言和平台的限制的。因为实际需要我们可能有时候要调用C#编写的WebService,这里我们需要添加一个Jar包的依赖。

    [KSOAP2点击下载](http://pan.baidu.com/s/1skKjvYX )

    需要了解的一些信息

    我们的WebService,建立以后,这么一个地址用来存放我们WebService的一些描述的信息。http://localhost:54603/NBAWebService.asmx?wsdl

    需要了解的信息

    1. WebService 命名空间
    2. 要执行的方法名
    3. SOAPAction
    4. SOAP 的版本.

    Note:

    • 因为涉及到了网络,一定要在子线程中使用。
    • 访问网络需要加上网络权限。
    • 传递参数的时候,参数的名称和顺序不能乱掉。

    WebService的代码

    我们将以下面的HelloWorld方法为例子

    namespace NBAWebService
    {
        /// <summary>
        /// Summary description for NBAWebService
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class NBAWebService : System.Web.Services.WebService
        {
    
            public MyLinqDataContext mldc = MethodHelper.GetLinq();
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
    
    		//****************************
        }
    }
    
    

    Android端调用代码

    
    /*
    * 用来调用C#的WebService
    * */
    public class ConnectionWebService {
        //WebService的地址
        //10.0.2.2 该地址是我们,android模拟器访问本机时使用的IP地址
        static final String URI = "http://10.0.2.2:8088/NBAWebService.asmx";
        //WebService 的命名空间
        static final String NAMESPACE = "http://tempuri.org/";
    
        /*
        * 调用WebService的方法
        * */
        public static String callMethod(String methodName, Map<String, String> parameterMap) throws IOException, XmlPullParserException {
            /*
            * SOAP的对象
            * namespace: 调用的命名空间
            * method name = 调用的方法名
            * */
            SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
    
            if (parameterMap != null) {
                //给SOAP对象传入我们需要的参数
                for (Map.Entry<String, String> item : parameterMap.entrySet()) {
                    soapObject.addProperty(item.getKey(), item.getValue());
                }
            }
    
            //设置我们的 WebService的SOAP版本
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    
            envelope.bodyOut = soapObject;
            //与.net兼容
            envelope.dotNet = true;
    
            //调用方法
            HttpTransportSE httpTransportSE = new HttpTransportSE(URI);
          
            httpTransportSE.call(NAMESPACE + "/" + methodName, envelope);
    
            //获取到返回值
            SoapPrimitive sp = (SoapPrimitive) envelope.getResponse();
            return sp.toString();
        }
    }
    
    
    分析

    这是我们调用这个WebService是使用的基本上不会改变的参数

    	 //WebService的地址
        //10.0.2.2 该地址是我们,android模拟器访问本机时使用的IP地址
        static final String URI = "http://10.0.2.2:8088/NBAWebService.asmx";
        //WebService 的命名空间
        static final String NAMESPACE = "http://tempuri.org/";
    

    我们方法传递的参数

    • methodName 需要调用的WebService的方法名
    • parameterMap 方法所需要的参数 格式为 key = 参数的名称 value = 参数的值
    public static String callMethod(String methodName, Map<String, String> parameterMap) throws IOException, XmlPullParserException
    

    指定WebService命名控件和需要调用的方法

    /*
    * SOAP的对象
    * namespace: 调用的命名空间
    * method name = 调用的方法名
    * */
    SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
    

    添加参数, parameterMap 为null 那么就表明该方法不需要参数。

    if (parameterMap != null) {
        //给SOAP对象传入我们需要的参数
        for (Map.Entry<String, String> item : parameterMap.entrySet()) {
            soapObject.addProperty(item.getKey(), item.getValue());
        }
    }
    

    SOAP的请求信息,该信息是由SoapSerializationEnvelope 对象描述的。

    //设置我们的 WebService的SOAP版本
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
    
    envelope.bodyOut = soapObject;
     //与.net兼容
    envelope.dotNet = true;
    

    创建HTTPTransportsSE 对象,构造参数中传递,WebService的URL

    call 方法调用我们的WebService方法, 传递我们的SOAPAction和我们的SoapSerializationEnvelope 对象

    一般来说 SOAPAction 是 命名空间+"/"+方法名.

     //设置我们的 WebService的URI
    //调用方法
    HttpTransportSE httpTransportSE = new HttpTransportSE(URI);
    httpTransportSE.call(NAMESPACE + "/" + methodName, envelope);
    

    获取到返回值。

    //获取到返回值
    SoapPrimitive sp = (SoapPrimitive) envelope.getResponse();
    return sp.toString();
    
    使用
     @Override
     public void run() {
         try {
             String jsonContent = ConnectionWebService.callMethod("HelloWorld", null);
             LogHelper.i(jsonContent);
         } catch (Exception e) {
             LogHelper.i("报错了");
         }
     }
    
  • 相关阅读:
    两个链表的第一个公共节点(Python and C++解法)
    第一个只出现一次的字符(Python and C++解法)
    丑数(Python and C++解法)
    最长不含重复字符的子字符串(Python and C++解法)
    礼物的最大值(Python and C++解法)
    把数字翻译成字符串(Python and C++解法)
    连续子数组的最大和(Python and C++解法)
    最小的k个数(Python and C++解法)
    数组中出现次数超过一半的数字(Python and C++解法)
    字符串的排列(Python and C++解法)
  • 原文地址:https://www.cnblogs.com/slyfox/p/7228670.html
Copyright © 2020-2023  润新知