package com.example.WebService;
import java.util.Map;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
/*
* WebService工具類(SoapObject or JSON)
* Author: S.Sams@MSN.COM
* Created by SamShum 2013-07-24 21:10
*/
public class WebServiceUtil {
private Boolean _isdotnet = true;
/*
* 設置當前WebServices是否支持 .net 的WebServices;
* @param dotNetWebService: 默認true;
*/
public WebServiceUtil setIsDotNet(Boolean dotNetWebService)
{
_isdotnet = dotNetWebService;
return this;
}
private int _setHttpTimeOut = 10* 1000;
/*
* 設置HTTP請求的時間,單位:秒;
* @param secondTime: 默認 10 s
*/
public WebServiceUtil setHttpTimeOut(int secondTime)
{
_setHttpTimeOut = secondTime;
return this;
}
private Boolean _isdebug = false;
/*
* 設置啟用HTTP的Debug模式
* @param isdebug: 默認 false
*/
public WebServiceUtil setIsDebug(Boolean isdebug)
{
_isdebug = isdebug;
return this;
}
private Boolean _iswritelog = false;
/*
* 是否輸出日志
* @param iswritelog: 默認 false
*/
public WebServiceUtil setOutLog(Boolean iswritelog)
{
_iswritelog = iswritelog;
return this;
}
/*
* 獲取WebService數據,並以字符形式返回。
* @param Url: WebService服務地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務的命名空間,可以WSDL數據中找到 (http://***.com.cn/)
* @param MethodName: WebService的調用函數方法名稱(getDataMethod)
* @param Maps: 請求服務需要提交的數據集
* @Return: 服務以字符類型返回請求數據
* @Exception: 寫入控制台日志
*/
public String GetString(String Url, String NameSpace, String MethodName, Map<String, String> RequestDatas){
return GetString(Url, NameSpace, MethodName, RequestDatas, null, null);
}
/*
* 獲取WebService數據,並以字符形式返回。
* @param Url: WebService服務地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務的命名空間,可以WSDL數據中找到 (http://***.com.cn/)
* @param MethodName: WebService的調用函數方法名稱(getDataMethod)
* @param Maps: 請求服務需要提交的數據集
* @param SoapHeadeName: 設置WebService的HTTP頭名稱
* @param SoapHeadeValues: 設置 SoapHeade 的數據集
* @Return: 服務以字符類型返回請求數據
* @Exception: 寫入控制台日志
*/
public String GetString(String Url, String NameSpace, String MethodName, Map<String, String> RequestDatas, String SoapHeadeName, Map<String, String> SoapHeadeValues) {
SoapObject soap = GetObject(Url, NameSpace, MethodName, RequestDatas, SoapHeadeName, SoapHeadeValues);
if(soap != null && soap.getPropertyCount() > 0)
{
String getResultString = soap.getProperty(0).toString();
if(_iswritelog) System.out.println("[Return Data] : "+ getResultString);
return getResultString;
}
return null;
}
/*
* 獲取WebService數據,返回SoapObject對象。
* @param Url: WebService服務地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務的命名空間,可以WSDL數據中找到 (http://***.com.cn/)
* @param MethodName: WebService的調用函數方法名稱(getDataMethod)
* @param Maps: 請求服務需要提交的數據集
* @Return: 服務返回SoapObject對象
* @Exception: 寫入控制台日志
*/
public SoapObject GetObject(String Url, String NameSpace, String MethodName, Map<String, String> RequestDatas) {
return GetObject(Url, NameSpace, MethodName, RequestDatas, null, null);
}
/*
* 獲取WebService數據,返回SoapObject對象。
* @param Url: WebService服務地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務的命名空間,可以WSDL數據中找到 (http://***.com.cn/)
* @param MethodName: WebService的調用函數方法名稱(getDataMethod)
* @param Maps: 請求服務需要提交的數據集
* @param SoapHeadeName: 設置WebService的HTTP頭名稱
* @param SoapHeadeValues: 設置 SoapHeade 的數據集
* @Return: 服務返回SoapObject對象
* @Exception: 寫入控制台日志
*/
public SoapObject GetObject(String Url, String NameSpace, String MethodName, Map<String, String> RequestDatas, String SoapHeadeName, Map<String, String> SoapHeadeValues) {
try {
SoapObject soap = new SoapObject(NameSpace, MethodName);
// 系統日志輸出
if(_iswritelog) System.out.println("[URL] : " + Url);
if(_iswritelog) System.out.println("[NameSpace] : " + NameSpace);
if(_iswritelog) System.out.println("[MethodName] : " + MethodName);
if(_iswritelog) System.out.println("[SOAP Action] : "+ NameSpace+MethodName);
// 設置WebService提交的數據集
if (RequestDatas != null && !RequestDatas.isEmpty()) {
for (Map.Entry<String, String> entry : RequestDatas.entrySet()) {
soap.addProperty(entry.getKey(), entry.getValue());
}
}
// 設置HTTP頭信息
Element[] header = null;
if(SoapHeadeName != null && SoapHeadeValues != null && !SoapHeadeValues.isEmpty())
{
header = new Element[1];
header[0] = new Element().createElement(NameSpace, SoapHeadeName);
for (Map.Entry<String, String> entry : SoapHeadeValues.entrySet()) {
Element element = new Element().createElement(NameSpace, entry.getKey());
element.addChild(Node.TEXT, entry.getValue());
header[0].addChild(Node.ELEMENT, element);
}
}
// 初始化數據請求
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = _isdotnet;
if(header != null)envelope.headerOut = header;
envelope.bodyOut = soap;
envelope.setOutputSoapObject(soap);
// 發起Web請求
HttpTransportSE http = new HttpTransportSE(Url, _setHttpTimeOut);
http.debug = _isdebug;
http.call(NameSpace+MethodName , envelope);
// 獲取Web請求結果, 數據需要從 result.getProperty(0) 獲取
SoapObject result = (SoapObject) envelope.bodyIn;
if(_iswritelog) System.out.println("[SOAP.getPropertyCount] : " + result.getPropertyCount());
return result;
} catch (Exception e) {
if(_iswritelog) System.err.println("[Http Exception] : " + e.getMessage());
}
return null;
}
}