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

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

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

1 
2
3
4
5
6
7
8
9 10
11
12
18
20
21
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

3. 寄宿服务

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

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

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

 

4. 使用ksoap2调用WCF

  去ksoap2官网 

   下载最新jar

 

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

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

  ISoapService

1 package junit.soap.wcf;2 3 import org.ksoap2.serialization.SoapObject;4 5 public interface ISoapService {6     SoapObject LoadResult();7 }

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

 

转载于:https://www.cnblogs.com/ruishuang208/p/4226739.html

你可能感兴趣的文章
httpd_Vhosts文件的配置
查看>>
php学习笔记
查看>>
普通求素数和线性筛素数
查看>>
React Router 4.0 基本使用
查看>>
PHP截取中英文混合字符
查看>>
【洛谷P1816 忠诚】线段树
查看>>
CDN 学习笔记
查看>>
电子眼抓拍大解密
查看>>
多线程---线程间的通信
查看>>
poj 1331 Multiply
查看>>
严重: 文档无效: 找不到语法。 at (null:2:19)
查看>>
tomcat7的数据库连接池tomcatjdbc的25个优势
查看>>
Html 小插件5 百度搜索代码2
查看>>
nodejs-Path模块
查看>>
P1107 最大整数
查看>>
EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器...
查看>>
监控CPU和内存的使用
查看>>
Ubuntu14.04设置开机自启动程序
查看>>
ios app 单元测试 自动化测试
查看>>
强连通tarjan模版
查看>>