android与.NET webservice(实战一)


前读: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);

原文地址:https://www.cnblogs.com/robinli/p/2715881.html