android调用.net wcf 服的方法
一、新建wcf服务
1 public class Service1 : IService1 2 { 3 public string GetData(int value) 4 { 5 return string.Format("You entered: {0}", value); 6 } 7 }
web.config 文件配置
1 <services> 2 <service name="WcfSrv.Service1"> 3 <host> 4 <baseAddresses> 5 <add baseAddress = "http://localhost:8732/WcfSrv/Service1/" /> 6 </baseAddresses> 7 </host> 8 <endpoint address ="" binding="basicHttpBinding" contract="WcfSrv.IService1"> 9 </endpoint> 10 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 11 </service> 12 </services>
注意红色部分为,默认的是wsHttpBinding ,这时android 调用wcf服务时会出现下面的异常
org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:1 in java.io.InputStreamReader@41308ae8)
然后把服务发布到本机的iis中
二、android 调用wcf 服务
1 static String SOAP_ACTION="http://tempuri.org/IService1/GetData"; 2 private static final String NAMESPACE = "http://tempuri.org/"; 3 private static String URL = http://10.0.2.2:8000/Service1.svc; 4 private static final String METHOD_NAME = "GetData"; 5 SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME); 6 soapObject.addProperty("value", 33333); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 7 envelope.dotNet = true; 8 envelope.setOutputSoapObject(soapObject); 9 String xmlMessage = ""; 10 try { 11 HttpTransportSE transport = new HttpTransportSE(URL); 12 transport.call(SOAP_ACTION, envelope); 13 SoapObject sb = (SoapObject) envelope.bodyIn; 14 xmlMessage = sb.toString(); } catch (IOException e) { 15 e.printStackTrace(); 16 } catch (XmlPullParserException e) { 17 e.printStackTrace(); 18 } 19 return xmlMessage;