android与.NET webservice

本文介绍如何使用Android客户端调用基于.NET Framework的Webservice,包括Webservice的创建及配置,以及Android端的具体调用代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前读:android链接本机的IIS需要用IPV4地址访问,其他地址访问可能访问不了

1、先用VS2008建立一个webservice,提供的方法如下

  [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]


    [SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]    

    public class Service1 : System.Web.Services.WebService

    {

         

        [WebMethod]

        public ArrayList  getStr()

        {

            ArrayList a = new ArrayList ();

            a.Add("haha");

            a.Add("bb");

            a.Add("cc");

            

            return a;

        }

}

2、调用结果如下

android与.NET webservice - 雨下晴川 - 小熊梦工厂
 

 Web.configSystem.web节点内加上如下子节点,防止发布到IIS后无法在网页中调用

android与.NET webservice - 雨下晴川 - 小熊梦工厂
 

出现以上错误需要添加如下节点,但是我们在android端调用的话不用配置此节点

<webServices>

      <protocols>

        <!--<add name= "HttpGet"/>-->

        <add   name"HttpGet"/>

      </protocols>

</webServices>

3、Android端的activity的核心代码如下(先添加如下提供的第三方库)

android与.NET webservice - 雨下晴川 - 小熊梦工厂
 

代码截图:

android与.NET webservice - 雨下晴川 - 小熊梦工厂
 

原文:

 

       String namespace="http://tempuri.org/";

    String methoName="getStr";

        

        SoapObject soapObject=new SoapObject(namespace, methoName);

        SoapSerializationEnvelope envelope=new    SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.dotNet=true;

        envelope.setOutputSoapObject(soapObject);//envelope.bodyOut=request;

        

        

       //或者 AndroidHttpTransport httpTranstation=new AndroidHttpTransport("http://192.168.172.82/Service1.asmx?wsdl");

        HttpTransportSE httpTranstation=new HttpTransportSE("http://192.168.172.82/Service1.asmx?wsdl");//红色部分可要可不要

        try {

            

            httpTranstation.call(namespace+methoName, envelope);

            SoapObject result=(SoapObject)envelope.getResponse();

            //

如果返回webservice返回类型不是数组类型,则上面这句话改成

Object result=envelope.getResponse();并且取Value即可

            

            int count=result.getPropertyCount();

            for(int index=0;index<count;index++){

                strList.add(result.getProperty(index).toString());

            }

            

            adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strList);

           provinceListView.setAdapter(adapter);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值