Android 调用WCF实例详解

Android 调用WCF实例

1. 构建服务端程序

?


1

2

3

4

5

6

7

8

9

10

11

12

13

using System.ServiceModel;

namespace yournamespace

{

  [ServiceContract(Name = "HelloService", Namespace = "http://www.master.haku")]

  public interface IHello

  {

    [OperationContract]

    string SayHello();

  }

}

<br>

?


1

2

3

4

5

6

7

8

9

10

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

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

<?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

?


1

2

3

4

5

6

7

8

9

package junit.soap.wcf;

import org.ksoap2.serialization.SoapObject;

public interface ISoapService {

  SoapObject LoadResult();

}

<br>

   HelloService

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

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;

  }

}

  测试程序

?


1

2

3

4

5

6

7

8

9

10

11

12

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客户端测试

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

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));

      }

    });

  }

}

<br>

7. 最后运行结果

安卓(Android)开发:利用ksoap2调用webservice服务,并自动生成客户端代理类代码

安卓调用Webservice和Java稍有不同,利用的是ksoap2这个jar包。之前这个jar包是发布在googlecode上面的目前项目已经移动到了github.io,我这里贴上的github官方网站,我也不知道这个github.io和github.com是不是一回事。我们可以在以下页面看到项目的总览:http://simpligility.github.io/ksoap2-android/index.html

1.下载ksoap2jar包

在如下页面进行下载:https://oss.sonatype.org/content/repositories/ksoap2-android-releases/com/google/code/ksoap2-android/ksoap2-android-assembly/

ksoap2项目的源码在这里,有兴趣的可以弄下来研究哦:

https://github.com/simpligility/ksoap2-android/

2.在Android Studio中进行配置

这一步简单,先放到lib文件夹下,然后再lib上点击右键,选择ADD AS LIB就可以了哦

3.利用网上的服务,自动生成ksoap2可用的webservice的客户端代理类

打开http://www.wsdl2code.com/pages/Home.aspx页面,在页面的右边填入你的webService的访问地址,然后选择生成的方式,我选的是Android Using kSoap2.如果你的webservice还没有发布,也可以直接上传其wsdl文件。

点击submit,此时要求登录,如果没有账号就注册一个,然后登陆,稍等一会这个工具就会为我们自动生成Webservice客户端代理类的代码了,点击下载

当然,自动生成的没有与jar运行环境啊什么的,可能使用的时候有些问题,至少包命名就得改成你自己的,所以,我们再简单的修改一下这些代码就可以直接使用了,省去了我们手动写客户端代理类的麻烦,是不是很方便啊。

原文地址:https://www.cnblogs.com/Alex80/p/11111891.html

时间: 2024-07-29 15:45:48

Android 调用WCF实例详解的相关文章

Android手机自动化测试实例详解

手机自动化框架也比较多,针对ios,android两个不同的平台,最后我挑选了appium,它是利用webdriver来进行驱动测试的,这个框架我比较熟悉,而且它支持两个平台.于是我就针对这两个不同的平台进行了分别的搭建与测试,IOS平台的环境搭建没有问题,具体搭建方法见: http://blog.sina.com.cn/s/blog_68f262210102v0ta.html,而测试用例,由于现在我没有办法将ipa文件转化成app文件,所以测试用例还没有搞定.问题解决后,会发相应的文章的. 本

Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程

上一篇文章主要讲述了Android的TouchEvent的分发过程,其中有两个重要的函数:onInterceptTouchEvent和onTouchEvent,这两个函数可被重装以完成特定的逻辑.onInterceptTouchEvent的定义为于ViewGroup中,默认返回值为false,表示不拦截TouchEvent.onTouchEvent的定义位于View中,当ViewGroup要调用onTouchEvent时,会利用super.onTouchEvent.ViewGroup调用onTo

实例详解:反编译Android APK,修改字节码后再回编译成APK

本文详细介绍了如何反编译一个未被混淆过的Android APK,修改smali字节码后,再回编译成APK并更新签名,使之可正常安装.破译后的apk无论输入什么样的用户名和密码都可以成功进入到第二个Activity. 有时难免要反编译一个APK,修改其中的若干关键判断点,然后再回编译成一个全新的可用的apk,这完全是可实现的.若要完成上述工作,需要以下工具,杂家后面会把下载链接也附上.这些软件截止本文发布时,经过杂家确认都是最新的版本. 1.APK-Multi-Toolv1.0.11.zip 用它

Android学习Scroller(五)——详解Scroller调用过程以及View的重绘

MainActivity如下: package cc.ww; import android.os.Bundle; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.app.Activity;

Cocos2d-x 3.X手游开发实例详解

Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰然网创始人杨雍力荐) 于浩洋 著   ISBN 978-7-121-23998-4 2014年9月出版 定价:59.00元 356页 16开 编辑推荐 以Cocos2d-x V3.0为框架全面讲解手游开发的知识和方法 以热门游戏2048.卡牌为例,完整再现手游的开发过程 Cocos2d-x作者之一林

Android四大组件--Activity详解

Android四大组件--Activity详解 分类: android android应用android开发 本文的主要内容包括1.activity的建立.配置和使用:2.activity的跳转和传值:3.startActivityForResult:4.activity的生命周期. 1.activity的建立.配置和使用 Activity是一个应用中的组件,它为用户提供一个可视的界面,方便用户操作,比如说拔打电话.照相.发邮件或者是浏览地图等.每个activity会提供一个可视的窗口,一般情况

Android开发之InstanceState详解

Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceState(),并且在介绍这两个方法之后,再分别来实现使用InstanceState保存和恢复数据功能.Android实现屏幕旋转异步下载效果这样两个示例. 首先来介绍onSaveInstanceState() 和 onRestoreInstanceState() .关于这两个方法,一些朋友可能在Andr

ANDROID L——Material Design详解(视图和阴影)

转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: 昨天凌晨Google刚刚确认Android L就是Android Lollipop(5.0). Google之前就已经提前推出了Android L Developer Preview(开发者预览版)来帮助开发者更快的了解Android特性,而不久前也推出了64位的模拟器镜像,而且首次搭载Android L系统的Nexus 6和 Nexus 9也即将上市. 相信And

Android开发之WebView详解

概述: 一个显示网页的视图.这个类是你可以滚动自己的Web浏览器或在你的Activity中简单地显示一些在线内容的基础.它使用了WebKit渲染引擎来显示网页,包括向前和向后导航的方法(通过历史记录),放大和缩小,执行文本搜索等. 需要注意的是:为了让你的应用能够使用WebView访问互联网和加载网页,你必须添加Internet的权限在Android Manifest文件中: <uses-permission android:name="android.permission.INTERNE