博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android调用WCF
阅读量:6078 次
发布时间:2019-06-20

本文共 4517 字,大约阅读时间需要 15 分钟。

hot3.png

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

3. 寄宿服务

  把网站发布到web服务器, 指定网站虚拟目录指向该目录
  如果你能够访问http://你的IP:端口/虚拟目录/服务.svc
  那么,恭喜你,你的服务端成功了! 
4. 使用ksoap2调用WCF
  去ksoap2官网

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

16131634_vd1V.png

转载于:https://my.oschina.net/u/582329/blog/169266

你可能感兴趣的文章
MyBatis学习总结——实现关联表查询(转)
查看>>
由于SSH配置文件的不匹配,导致的Permission denied (publickey)及其解决方法
查看>>
jqgrid 查询
查看>>
再谈.net的堆和栈---.NET Memory Management Basics
查看>>
BBS论坛(二十二)
查看>>
JS 的加密库简介
查看>>
Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权
查看>>
Virtualbox中Linux添加新磁盘并创建分区
查看>>
Spring ConfigurationClassPostProcessor Bean解析及自注册过程
查看>>
一步步教你轻松学主成分分析PCA降维算法
查看>>
socket bind 随机端口
查看>>
iOS 字典自动生成模型
查看>>
读jQuery之十(事件模块概述)
查看>>
仅img元素创建后不添加到文档中会执行onload事件
查看>>
全球知名汽车公司品牌
查看>>
Linux SSH远程文件/目录传输命令scp
查看>>
黄聪:WordPress判断当前用户是否为管理员登录
查看>>
Android service binder aidl 关系
查看>>
Redis数据结构详解(一)
查看>>
MySql配置方法,批处理进行MySql配置
查看>>