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点击下载

需要了解的一些信息

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

需要了解的信息

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

Note:

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

997244-20170724141337477-713369820.png

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("报错了");
     }
 }

转载于:https://www.cnblogs.com/slyfox/p/7228670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值