Android Studio connects WCF via KSOAP2

Source: Internet
Author: User

< environment >

Android studio:1.1

Visual studio:2013

ksoap2:3.4

Run: SERVER2012R2 + IIS8.0 + xiaomi4.4.2

< purpose >

Build a simple WCF service HelloService through VS, and provide a service method Sayhello,as connect to the service through KSOAP2 and call the SayHello method, and get the return data.

Reference:

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

< process >

VS, set up WCF service Hello.svc, with the following code:

 using   System.ServiceModel;  namespace   WCF. Demo.service {[ServiceContract (Name  =   HelloService  , Namespace = "  Span style= "color: #800000;" >http://www.longkai.com   )]  Span style= "color: #0000ff;" >public  interface   Ihello {[Operationcon Tract]  string  SayHello (string  Span style= "color: #000000;"    > word); }}

The code implemented is IHello.cs as follows:

namespace WCF. Demo.service {    publicclass  Hello:ihello {        publicstring SayHello (string  word) {            return"" + Word;}}    }

Right-click Web. config and edit the WCF configuration. Set up advanced Services behavior, create new service, create a new binding configuration, and finally publish to IIS. The configuration file code is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <system.web>    <compilationDebug= "true" />    <customErrorsMode= "Off"/>  </system.web>  <System.ServiceModel>    <servicehostingenvironment>      <serviceactivations>        <Addrelativeaddress= "Hello.svc"Service= "WCF." Demo.Service.Hello "/>      </serviceactivations>    </servicehostingenvironment>    <Bindings>      <BasicHttpBinding>        <bindingname= "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">          <ReaderquotasmaxDepth= "+"Maxstringcontentlength= "8192"Maxarraylength= "16384"Maxbytesperread= "4096"Maxnametablecharcount= "16384" />          <SecurityMode= "None">            <TransportclientCredentialType= "None"Proxycredentialtype= "None"Realm="" />            <messageclientCredentialType= "UserName"Algorithmsuite= "Default" />          </Security>        </binding>      </BasicHttpBinding>    </Bindings>        <Services>      <Servicename= "WCF." Demo.Service.Hello "behaviorconfiguration= "ServiceBehavior">        <Host>          <baseaddresses>            <Addbaseaddress= "Http://192.168.1.2:9999/Hello"/>          </baseaddresses>        </Host>        <Endpointbinding= "BasicHttpBinding"Contract= "WCF." Demo.Service.IHello ">          <Identity>            <DNSvalue= "localhost" />          </Identity>        </Endpoint>      </Service>    </Services>    <Behaviors>      <servicebehaviors>        <Behaviorname= "ServiceBehavior">          <Servicemetadatahttpgetenabled= "true" />          <ServicedebugIncludeexceptiondetailinfaults= "true" />        </Behavior>      </servicebehaviors>    </Behaviors>  </System.ServiceModel></Configuration>

Enter the code in as:

//creates a new interface that specifically reads the Soapobject object returned by WCF Public InterfaceIsoapservice {soapobject loadresult ();} Public classHelloServiceImplementsIsoapservice {Private Static FinalString NameSpace = "http://www.longkai.com";Private Static FinalString URL = "Http://192.168.1.2:9999/Hello.svc";Private Static FinalString soap_action = "Http://www.longkai.com/HelloService/SayHello";Private Static FinalString MethodName = "SayHello";PrivateString Word; PublicHelloService (String word) { This. Word =Word; } Publicsoapobject Loadresult () {soapobject soapobject=NewSoapobject (NameSpace, MethodName); Soapobject.addproperty ("Word", Word); Soapserializationenvelope Envelope=NewSoapserializationenvelope (SOAPENVELOPE.VER11);//versionEnvelope.bodyout =Soapobject; Envelope.dotnet=true;    Envelope.setoutputsoapobject (Soapobject); Httptransportse Trans=NewHttptransportse (URL); Trans.debug=true;//using the Debug featureTry{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;returnresult; }}

To enter code in the main thread:

New Thread (new  Runnable () {@Override  publicvoidnew HelloService ( "Jack"= service.    Loadresult ();  System.out.println ("The data returned by WCF is:" + result.getproperty (0)); }});

Android Studio connects WCF via KSOAP2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.