Android调用C#的WebService

Android调用C#写的WebService

学习自:html

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

运行环境

Win10android

VS 2015web

Android Studio 2.2.3json

KSOAP2的Jar包c#

Overview

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

KSOAP2点击下载ide

须要了解的一些信息

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

须要了解的信息this

  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("报错了");
     }
 }
相关文章
相关标签/搜索