-
Notifications
You must be signed in to change notification settings - Fork 802
/
WebServiceUtils.java
119 lines (107 loc) · 4.47 KB
/
WebServiceUtils.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.jingewenku.abrahamcaijin.commonutil;
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.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Description:主要功能:WebService网络工具类
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil
* @author: AbrahamCaiJin
* @date: 2017年05月22日 11:47
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
public class WebServiceUtils {
public static final String WEB_SERVER_URL = "";
// 含有3个线程的线程池
private static final ExecutorService executorService = Executors
.newFixedThreadPool(3);
// 命名空间
private static final String NAMESPACE = "";
/**
*请求网络数据
* @param url
* WebService服务器地址
* @param methodName
* WebService的调用方法名
* @param properties
* WebService的参数
* @param webServiceCallBack
* 回调接口
*/
public static void callWebService(String url, final String methodName,
HashMap<String, String> properties,
final WebServiceCallBack webServiceCallBack) {
// 创建HttpTransportSE对象,传递WebService服务器地址
final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
// 创建SoapObject对象
SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
// SoapObject添加参数
//写真实的变量名也没有用,传递参数必须是arg开头,必须是arg0 ,arg1 一直拍下去
if (properties != null) {
for (Iterator<Entry<String, String>> it = properties.entrySet()
.iterator(); it.hasNext();) {
Map.Entry<String, String> entry = it.next();
soapObject.addProperty(entry.getKey(), entry.getValue());
}
}
// 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号
//tomcat 7.055 for 64 jdk 1.6 for 64 web3.0 这里写 ver12 ,如果是ver11会报 http 415错误
final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
// 设置是否调用的是.Net开发的WebService
soapEnvelope.bodyOut = soapObject;
soapEnvelope.dotNet = false;//如果调用的是.Net的WebService,这里为true,否则为false,不然调用会不成功
soapEnvelope.setOutputSoapObject(soapObject);
httpTransportSE.debug = true;
// 用于子线程与主线程通信的Handler
final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 将返回值回调到callBack的参数中
webServiceCallBack.callBack((Object) msg.obj);
}
};
// 开启线程去访问WebService
executorService.submit(new Runnable() {
@Override
public void run() {
Object resultSoapObject = null;
try {
httpTransportSE.call(WEB_SERVER_URL + methodName, soapEnvelope);
if (soapEnvelope.getResponse() != null) {
// 获取服务器响应返回的SoapObject
resultSoapObject = (Object) soapEnvelope.bodyOut;
}
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} finally {
// 将获取的消息利用Handler发送到主线程
mHandler.sendMessage(mHandler.obtainMessage(0,
resultSoapObject));
}
}
});
}
public interface WebServiceCallBack {
public void callBack(Object result);
}
}