一、建立wcf服務
1 public class Service1 : IService12 {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;