Android调用WCF服务

Posted petewell

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android调用WCF服务相关的知识,希望对你有一定的参考价值。

文章目录

本篇博客是android调用WCF服务的简单实例。

实例

本例以简单的一个创建通知的WCF接口为例讲述Android与WCF的交互。
首先可使用WcfTestClient测试下WCF接口的CreateNotice方法,可参照上篇Java调用WCF的博客。
新建WCFService调用WCF的工具类如下

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.carlos.wcf;

import android.os.Handler;
import android.os.Message;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


* 访问WCF工具类
* Created by carlos on 2015/11/28.
*/
public class {
public static final String WCF_URL="";//引号类改为自己的
public static final String NAMESPACE="";//引号类改为自己的
public static final String SOAP="";//引号类改为自己的
private static final ExecutorService executorService= Executors.newFixedThreadPool(3);


* 调用WCF方法
* @param methodName
* @param properties
* @param wcfServiceCallBack
*/
public static void callWCFService(final String methodName,Map properties, final WCFServiceCallBack
wcfServiceCallBack){
// 创建HttpTransportSE对象,传递WCFService服务器地址
final HttpTransportSE httpTransportSE=new HttpTransportSE(WCF_URL);
SoapObject soapObject=new SoapObject(NAMESPACE,methodName);
if(properties!=null){
for(Iterator<Map.Entry<String,Object>> it=properties.entrySet().iterator();it.hasNext();){
Map.Entry<String,Object> entry=it.next();
soapObject.addProperty(entry.getKey(),entry.getValue());
}
}

// 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号
final SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.bodyOut=soapObject;
// 设置是否调用的是.Net开发的WCFService
soapEnvelope.setOutputSoapObject(soapObject);
soapEnvelope.dotNet=true;
httpTransportSE.debug=true;

// 用于子线程与主线程通信的Handler
final Handler mHandler=new Handler(){

public void handleMessage(Message msg) {
super.handleMessage(msg);
// 将返回值回调到callBack的参数中
wcfServiceCallBack.callBack((SoapObject) msg.obj);
}
};
// 开启线程去访问WCFService
executorService.submit(new Runnable() {

public void run() {
SoapObject resultSoapObject=null;
try {
httpTransportSE.call(SOAP+methodName,soapEnvelope);
if(soapEnvelope.getResponse()!=null){
resultSoapObject= (SoapObject) soapEnvelope.bodyIn;
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}finally {
// 将获取的消息利用Handler发送到主线程
mHandler.sendMessage(mHandler.obtainMessage(0,resultSoapObject));
}
}
});
}


* 回调接口
*/
public interface WCFServiceCallBack{
public void callBack(SoapObject result);
}
}

MainActivity类里面调用该方法打印出返回的WCF结果

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
57
58
59
60
package com.carlos.wcf;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

private Button b_get_WCFResult;
private final static String TAG="WCFResult";


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b_get_WCFResult= (Button) findViewById(R.id.b_get_WCFResult);

b_get_WCFResult.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
getWCFResult();
}
});

}

private void getWCFResult(){
HashMap<String,Object> map=new HashMap<>();
map.put("title", "提醒");
map.put("content", "晚上写篇博客");
WCFService.callWCFService("CreateNotice", map, new WCFService.WCFServiceCallBack() {

public void callBack(SoapObject result) {
if(result!=null){
Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_SHORT).show();
Log.i(TAG, result.toString());

SoapObject soapObject=(SoapObject)result.getProperty("result");
Log.i(TAG,soapObject.toString());
if(soapObject.toString().equals("anyType{}")){
Log.i(TAG,"没有结果");
}else{
for(int i=0;i<soapObject.getPropertyCount();i++){
Log.i("WCFResult",soapObject.getProperty(i).toString());
}
}

}
}
});
}
}

点击获取WCF结果的按钮后控制台输出的结果
技术图片

原文:大专栏  Android调用WCF服务


以上是关于Android调用WCF服务的主要内容,如果未能解决你的问题,请参考以下文章

Android 调用.Net WCF服务 .

想将图像从 android 发布到 WCF 休息服务

Android 调用WCF实例详解

调用 WCF Web 服务时出现错误的 Json 响应

从片段调用 Google Play 游戏服务

使用具有复杂对象的 KSoap2 调用 WCF 服务。 WCF 接收空值