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)]
publicclassService1:System.Web.Services.WebService
{
[WebMethod]
publicArrayListgetStr()
{
ArrayLista=newArrayList();
a.Add("haha");
a.Add("bb");
a.Add("cc");
returna;
}
}
2、调用结果如下
Web.config的System.web节点内加上如下子节点,防止发布到IIS后无法在网页中调用
出现以上错误需要添加如下节点,但是我们在android端调用的话不用配置此节点
<webServices>
<protocols>
<!--<addname="HttpGet"/>-->
<addname="HttpGet"/>
</protocols>
</webServices>
3、Android端的activity的核心代码如下(先添加如下提供的第三方库)
代码截图:
原文:
Stringnamespace="http://tempuri.org/";
StringmethoName="getStr";
SoapObjectsoapObject=newSoapObject(namespace,methoName);
SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(soapObject);//envelope.bodyOut=request;
//或者AndroidHttpTransporthttpTranstation=newAndroidHttpTransport("http://192.168.172.82/Service1.asmx?wsdl");
HttpTransportSEhttpTranstation=newHttpTransportSE("http://192.168.172.82/Service1.asmx?wsdl");//红色部分可要可不要
try{
httpTranstation.call(namespace+methoName,envelope);
SoapObjectresult=(SoapObject)envelope.getResponse();
//
如果返回webservice返回类型不是数组类型,则上面这句话改成
Objectresult=envelope.getResponse();并且取Value即可
intcount=result.getPropertyCount();
for(intindex=0;index<count;index++){
strList.add(result.getProperty(index).toString());
}
adapter=newArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strList);
provinceListView.setAdapter(adapter);
更多相关文章
- AndRoid完全退出程序
- [Innost]Android深入浅出之Binder机制
- Android发送短信方法实例详解
- Android中ListView中Item的设置
- 利用Handler来更新android的UI
- android 包管理系统分析
- Android下打印调用栈
- Java工程中调用Android库出现“Stub!”错误
- 如何去除launcher 上默认的 google search bar.