Android调用WCF服务

知识的领域是无限的,我们的学习也是无限期的。这篇文章主要讲述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调用WCF服务


    推荐阅读