Android 调用WCF实例详解

 更新时间:2016年11月26日 11:49:52   投稿:lqh  
这篇文章主要介绍了Android 调用WCF实例详解的相关资料,这里提供了实例代码及实现效果图,需要的朋友可以参考下

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. 最后运行结果

 

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

相关文章

  • kotlin中使用ViewBinding绑定控件的方法

    kotlin中使用ViewBinding绑定控件的方法

    View Binding是Android Studio 3.6推出的新特性,主要用于减少findViewById的冗余代码,但内部实现还是通过使用findViewById,这篇文章主要介绍了kotlin中使用ViewBinding绑定控件,需要的朋友可以参考下
    2024-03-03
  • Android编程实现的自定义弹窗(PopupWindow)功能示例

    Android编程实现的自定义弹窗(PopupWindow)功能示例

    这篇文章主要介绍了Android编程实现的自定义弹窗(PopupWindow)功能,结合简单实例形式分析了Android自定义弹窗实现方法与相关注意事项,需要的朋友可以参考下
    2017-03-03
  • Android打包上传AAR文件到Maven仓库的示例

    Android打包上传AAR文件到Maven仓库的示例

    这篇文章主要介绍了Android打包上传AAR文件到Maven仓库的示例,帮助大家更好的理解和学习使用Android开发,感兴趣的朋友可以了解下
    2021-03-03
  • 关于RxJava的一些特殊用法小结

    关于RxJava的一些特殊用法小结

    RxJava 是一个响应式编程框架,采用观察者设计模式。下面这篇文章主要总结介绍了一些关于RxJava的特殊用法,需要的朋友可以参考借鉴,下面跟着小编一起来学习学习吧。
    2017-05-05
  • Android自定义view实现滚动选择控件详解

    Android自定义view实现滚动选择控件详解

    最近在开发中需要实现滚动进行类别的选择,也就是我们所说的滚动选择器,这里我们自定义来实现这个功能,这篇文章主要介绍了Android自定义view实现滚动选择控件
    2022-11-11
  • 基于android实现五子棋开发

    基于android实现五子棋开发

    这篇文章主要为大家详细介绍了基于android实现五子棋开发,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • Android使用ImageView实现支持手势缩放效果

    Android使用ImageView实现支持手势缩放效果

    这篇文章主要介绍了Android使用ImageView实现支持手势缩放效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Android开发之将两张图片合并为一张图片的方法

    Android开发之将两张图片合并为一张图片的方法

    这篇文章主要介绍了Android开发之将两张图片合并为一张图片的方法,涉及Android基于Canvas类操作图片的相关技巧,需要的朋友可以参考下
    2016-01-01
  • Flutter进阶之实现动画效果(一)

    Flutter进阶之实现动画效果(一)

    这篇文章主要为大家详细介绍了Flutter实现动画效果的第一篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android开发之时间日期组件用法实例

    Android开发之时间日期组件用法实例

    这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
    2014-08-08

最新评论