Android invoke WCF instance detailed _android

Source: Internet
Author: User
Tags soap serialization

Android invoke WCF Instance

1. Building a server-side program

Using System.ServiceModel;

Namespace YourNameSpace
{
  [ServiceContract (Name = "HelloService", Namespace = "Http://www.master.haku")] Public
  Interface Ihello
  {
    [OperationContract]
    string SayHello ();
  }
}


Namespace YourNameSpace
{public
  class Yourservice  
  {public
   string SayHello (string words)
   { return
      "Hello" + words;
   }
  }

2. Building an IIS Web site host

Yourservice.svc

< % @ServiceHost debug= "true" service= "Yournamespace.yourservice"%>

Web.config

<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <system.serviceModel> < servicehostingenvironment> <serviceactivations > <add relativeaddress= "yourservice.svc" service= "YourNa" Mespace.yourservice "/> </serviceactivations > </servicehostingenvironment > <bindings> ;basichttpbinding> <binding name= "basichttpbindingcfg" closetimeout= "00:01:00" openTimeout= "00:01:00" rece
      ivetimeout= "00:10:00" sendtimeout= "00:01:00" bypassproxyonlocal= "false" hostnamecomparisonmode= "StrongWildcard" Maxbufferpoolsize= "524288" maxreceivedmessagesize= "2147483647" messageencoding= "Text" textencoding= "Utf-8" UseDefa Ultwebproxy= "true" allowcookies= "false" > <readerquotas maxdepth= "maxstringcontentlength=" 8192
      Aylength= "16384" maxbytesperread= "4096" maxnametablecharcount= "16384"/> <security "None" > <transport clientcredentialtype= "None"Proxycredentialtype= "None" realm= ""/> <message clientcredentialtype= "UserName" algorithmsuite= "Defaul T "/> </security> </binding> </basicHttpBinding> </bindings> <services&gt
   ; <service name= "Yournamespace.yourservice" behaviorconfiguration= "ServiceBehavior" >  

3. Boarding Service

Publish a Web site to a Web server, specify a Web site virtual directory to point to the directory

If you can access http://your IP: port/virtual directory/service. svc

Well, congratulations, your service side is successful!

4. Invoke WCF using KSOAP2

Go to KSOAP2 official website

Http://code.google.com/p/ksoap2-android/Download the latest jar

5. Create a new Java project in Eclipse and test your service

Creates a new interface for specifically reading Soapobject objects returned by WCF

Isoapservice



Package JUNIT.SOAP.WCF;

Import Org.ksoap2.serialization.SoapObject;

Public interface Isoapservice {
  soapobject loadresult ();
}


HelloService



Package JUNIT.SOAP.WCF;
Import java.io.IOException;
Import Org.ksoap2.SoapEnvelope;
Import Org.ksoap2.serialization.SoapObject;
Import Org.ksoap2.serialization.SoapSerializationEnvelope;
Import Org.ksoap2.transport.HttpTransportSE;

Import org.xmlpull.v1.XmlPullParserException;
  public class HelloService implements Isoapservice {private static final String NameSpace = "Http://www.master.haku";
  private static final String URL = "http://your server/virtual directory/Your service. svc";
  private static final String soap_action = "http://www.master.haku/your service/sayhello";
  
  Private static final String methodname = "SayHello";
  
  private String words;
  Public HelloService (String words) {this.words = words;
    Public Soapobject Loadresult () {Soapobject soapobject = new Soapobject (NameSpace, MethodName);
    
    Soapobject.addproperty ("words", words); Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER11);
    Version envelope.bodyout = Soapobject; EnvelOpe.dotnet = true;
    
    Envelope.setoutputsoapobject (Soapobject);
    Httptransportse trans = new Httptransportse (URL); Trans.debug = true;
      Use the Debug feature 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;

 }
}

Test program

Package JUNIT.SOAP.WCF;

Import Org.ksoap2.serialization.SoapObject;

public class Hellowcftest {public
  static void Main (string[] args) {
    HelloService service = new HelloService ("Mast ER HaKu ");
    Soapobject result = Service. Loadresult ();
    
    SYSTEM.OUT.PRINTLN ("WCF returned data is:" + result.getproperty (0));
  }


Tested successfully

Run Result:

Hello Master HaKu

6. Android Client Test

Package DAVID.ANDROID.WCF;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.Toast;

Import Org.ksoap2.serialization.SoapObject;
  public class Androidwcfdemoactivity extends activity {private Button mButton1;

  private TextView text; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    MButton1 = (Button) Findviewbyid (R.id.mybutton1);

    Text = (TextView) This.findviewbyid (r.id.show);
        
         Mbutton1.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View v) {
                HelloService service = new HelloService ("Master HaKu"); Soapobject result = Service.

        Loadresult (); Text.settext ("WCF returned data is:" + result.getproperty (0));
  }
    });

 }
}

7. Final Run Results

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.