欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 调用WCF实例详解

程序员文章站 2024-02-28 09:21:28
android 调用wcf实例 1. 构建服务端程序 using system.servicemodel; namespace yournamespa...

android 调用wcf实例

1. 构建服务端程序

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. 构建iis网站宿主

  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="yournamespace.yourservice"/>
   </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="yournamespace.yourservice" behaviorconfiguration="servicebehavior">
    <host>
     <baseaddresses>
      <add baseaddress="http://localhost:59173/yourservice"/>
     </baseaddresses>
    </host>
    <endpoint binding="basichttpbinding" contract="yournamespace.你的服务契约接口">
     <identity>
      <dns value="localhost" />
     </identity>
    </endpoint>
   </service>
  </services>

  <behaviors>
   <servicebehaviors>
    <behavior name="servicebehavior">
     <servicemetadata httpgetenabled="true" />
     <servicedebug includeexceptiondetailinfaults="true" />
    </behavior>
   </servicebehaviors>
  </behaviors>
 </system.servicemodel>
 <system.web>
  <compilation debug="true" />
 </system.web>
</configuration>

3. 寄宿服务

  把网站发布到web服务器, 指定网站虚拟目录指向该目录

  如果你能够访问http://你的ip:端口/虚拟目录/服务.svc

  那么,恭喜你,你的服务端成功了! 

4. 使用ksoap2调用wcf

  去ksoap2官网

  http://code.google.com/p/ksoap2-android/ 下载最新jar

 5. 在eclipse中新建一个java项目,测试你的服务

  新建一个接口, 用于专门读取wcf返回的soapobject对象

  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://你的服务器/虚拟目录/你的服务.svc";
  private static final string soap_action = "http://www.master.haku/你的服务/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); // 版本
    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;
  }
}

  测试程序

package junit.soap.wcf;

import org.ksoap2.serialization.soapobject;

public class hellowcftest {
  public static void main(string[] args) {
    helloservice service = new helloservice("master haku");
    soapobject result = service.loadresult();
    
    system.out.println("wcf返回的数据是:" + result.getproperty(0));
  }
}

   经过测试成功

   运行结果:

   hello master haku

6. android客户端测试

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 when the activity is first created. */
  @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返回的数据是:" + result.getproperty(0));
      }
    });
  }
}


7. 最后运行结果

 Android 调用WCF实例详解

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!