一、WebService和SOAP介绍

​ WebService是一种基于SOAP协议的远程调用标准,是跨平台的,可以在不同语言中使用的。WebServie使用XML用来编解码数据,并使用SOAP来传输数据。

​ SOAP是基于XML的简易协议,使用XML进行编码,使用HTTP进行数据的交互,它是一种用于网络服务的协议,独立于平台,独立于语言。在Android SDK中没有提供与WebService通信的库,所以我们需要使用第三方类库(KSOAP2)建立SOAP客户端与WebService通信。

二、KSOAP2的使用

  1. ksoap2官网 ksoap2源码 WebService教程 SOAP教程
  2. ksoap2步骤
  • 使用WebService的命名空间和调用的方法名构成SoapObject:
1
SoapObject request = new Soapobject(nameSpace,method);
  • 设置调用方法的参数(可选)
1
request.addProperty("param","value");
  • 构建SOAP请求体:
1
2
3
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request();//SoapObject对象
envelope.dotNet = true;//是否是.NET WebService

​ SoapEnvelope.VER11为SOAP协议的版本号,该版本号根据服务端WebService的版本号设置,现在有VER10,VER11,VER12。

  • 创建HttpTransportSE对象并调用。通过HttpTransportSE类的构造方法可以指定WebService的URL:
1
2
HttpTransportSE transport = new HttpTransportSE(request.getURL(), out_times);
transport.call(request.getSoapAction(), envelope); //call方法调用WebService
  • 得到返回值:
1
SoapObject soapObject = (SoapObject) envelope.getResponse();

下面是简单封装的ksoap调用:

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
92
93
94
95
public class SoapSend implements Callable<SoapResponse> {
private SoapRequest soapRequest;
public SoapSend(SoapRequest soapRequest) {
this.soapRequest = soapRequest;
}
@Override
public SoapResponse call() {
Object result = sendSoap(soapRequest);
if (result != null && result instanceof SoapObject) {
return new SoapResponse(result);
}
return null;
}
private Object sendSoap(SoapRequest request) {
try {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request.getParameters();
envelope.dotNet = true;
envelope.setOutputSoapObject(request.getParameters());
new MarshalBase64().register(envelope);
int out_times = 20000;
HttpTransportSE transport = new HttpTransportSE(request.getURL(), out_times);
transport.call(request.getSoapAction(), envelope);
return envelope.getResponse();
} catch (IOException | XmlPullParserException e) {
soapRequest.onNetError(e);
}
return null;
}
}
public abstract class SoapRequest{
private String URL;
private String nameSpace;
private String methodName;
private String soapAction;
private SoapObject parameters;
public abstract void onNetError(Exception e);
private ExecutorService executorService = Executors.newCachedThreadPool();
protected SoapRequest(String URL, String nameSpace, String methodName) {
this.URL = URL;
this.nameSpace = nameSpace;
this.methodName = methodName;
this.soapAction = nameSpace + methodName;
parameters = new SoapObject(nameSpace, methodName);
}
protected void addParameter(String name, boolean value) {
addParameter(name, String.valueOf(value));
}
protected void addParameter(String name, int value) {
addParameter(name, String.valueOf(value));
}
protected void addParameter(String name, String value) {
if (name == null || value == null) {
return;
}
parameters.addProperty(name, value);
}
public SoapObject getParameters() {
return parameters;
}
public String getSoapAction() {
return soapAction;
}
public String getURL() {
return URL;
}
public SoapResponse sendRequest() {
try {
FutureTask<SoapResponse> futureTask = new FutureTask<>(new SoapSend(this));
executorService.submit(futureTask);
return futureTask.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
}
}