android与.NET webservice(实战一)
2012-10-08 22:24 robinli 阅读(358) 评论(0) 编辑 收藏 举报前读: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 - 雨下晴川 - 小熊梦工厂](http://img0.ph.126.net/w1dy-pp54UOy6g20Da2fvQ==/1556275146250507070.jpg)
Web.config的System.web节点内加上如下子节点,防止发布到IIS后无法在网页中调用
![android与.NET webservice - 雨下晴川 - 小熊梦工厂](http://img5.ph.126.net/M6IdD15gSC_WB2fwr6ypPg==/2395915001778392345.jpg)
出现以上错误需要添加如下节点,但是我们在android端调用的话不用配置此节点
<webServices>
<protocols>
<!--<add name= "HttpGet"/>-->
<add name= "HttpGet"/>
</protocols>
</webServices>
3、Android端的activity的核心代码如下(先添加如下提供的第三方库)
![android与.NET webservice - 雨下晴川 - 小熊梦工厂](http://img9.ph.126.net/rzm0yr5NK3juZUhDTS-uYQ==/1167558203413090719.jpg)
代码截图:
![android与.NET webservice - 雨下晴川 - 小熊梦工厂](http://img4.ph.126.net/SwWIa1xMIyfNQCZTP1eYhA==/2717077950205252984.jpg)
原文:
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);