<环境>
Android Studio:1.1
Visual Studio:2013
Ksoap2:3.4
运行:Server2012R2 + IIS8.0 + xiaomi4.4.2
<目的>
通过VS建立一个简单的WCF服务HelloService,并提供服务方法SayHello,AS中通过Ksoap2连接到该服务并调用SayHello方法,并得到返回数据。
参考:
http://blog.sina.com.cn/s/blog_87131d9a0101rmwg.html
http://www.cnblogs.com/davidgu/archive/2012/05/16/2504182.html
http://blog.sina.com.cn/s/blog_72a0e0b00101j2s7.html
<过程>
VS中建立WCF服务Hello.svc,代码如下:
using System.ServiceModel; namespace WCF.Demo.Service { [ServiceContract(Name = "HelloService", Namespace = "http://www.longkai.com")] public interface IHello { [OperationContract] string SayHello(string word); } }
实现的代码IHello.cs如下:
namespace WCF.Demo.Service { public class Hello : IHello { public string SayHello(string word) { return "HelloWorld " + word; } } }
右击Web.config,编辑WCF配置。设置高级->服务行为、新建服务、新建绑定配置等,最后发布到IIS上。配置文件代码如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" /> <customErrors mode="Off"/> </system.web> <system.serviceModel> <serviceHostingEnvironment> <serviceActivations > <add relativeAddress="Hello.svc" service="WCF.Demo.Service.Hello"/> </serviceActivations > </serviceHostingEnvironment > <bindings> <basicHttpBinding> <binding name="BasicHttpBindingCfg" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <services> <service name="WCF.Demo.Service.Hello" behaviorConfiguration="ServiceBehavior"> <host> <baseAddresses> <add baseAddress="http://192.168.1.2:9999/Hello"/> </baseAddresses> </host> <endpoint binding="basicHttpBinding" contract="WCF.Demo.Service.IHello"> <identity> <dns value="localhost" /> </identity> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
在AS中输入代码:
//新建一个接口, 用于专门读取WCF返回的SoapObject对象 public interface ISoapService { SoapObject LoadResult(); } public class HelloService implements ISoapService { private static final String NameSpace = "http://www.longkai.com"; private static final String URL = "http://192.168.1.2:9999/Hello.svc"; private static final String SOAP_ACTION = "http://www.longkai.com/HelloService/SayHello"; private static final String MethodName = "SayHello"; private String word; public HelloService(String word) { this.word = word; } public SoapObject LoadResult() { SoapObject soapObject = new SoapObject(NameSpace, MethodName); soapObject.addProperty("word", word); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // 版本 envelope.bodyOut = soapObject; envelope.dotNet = true; envelope.setOutputSoapObject(soapObject); HttpTransportSE trans = new HttpTransportSE(URL); trans.debug = true; // 使用调试功能 try { trans.call(SOAP_ACTION, envelope); System.out.println("Call Successful!"); } catch (IOException e) { System.out.println("IOException"); e.printStackTrace(); } catch (XmlPullParserException e) { System.out.println("XmlPullParserException"); e.printStackTrace(); } SoapObject result = (SoapObject) envelope.bodyIn; return result; } }
在主线程中输代码:
Thread thread = new Thread(new Runnable() { @Override public void run() { HelloService service = new HelloService("Jack"); SoapObject result = service.LoadResult(); System.out.println("WCF返回的数据是:" + result.getProperty(0)); } });
时间: 2024-10-07 10:15:30