android wsdl封装,《android 解析WebService》
android端解析WebService我采用的是ksoap來完成的。ksoap相對(duì)來說也是比較簡單的。代碼如下
package com.lv.test;
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;
import java.util.Map;
public class WebServiceUtil {
private Boolean _isdotnet = false;
/*
* 設(shè)置當(dāng)前WebServices是否支持 .net 的WebServices;
* @param dotNetWebService: .net默認(rèn)true;java默認(rèn)是false
*/
public WebServiceUtil setIsDotNet(boolean dotNetWebService) {
_isdotnet = dotNetWebService;
return this;
}
private int _setHttpTimeOut = 10 * 1000;
/*
* 設(shè)置HTTP請(qǐng)求的時(shí)間,單位:秒;
* @param secondTime: 默認(rèn) 10 s
*/
public WebServiceUtil setHttpTimeOut(int secondTime) {
_setHttpTimeOut = secondTime;
return this;
}
private boolean _isdebug = false;
/*
* 設(shè)置啟用HTTP的Debug模式
* @param isdebug: 默認(rèn) false
*/
public WebServiceUtil setIsDebug(boolean isdebug) {
_isdebug = isdebug;
return this;
}
/*
* 獲取WebService數(shù)據(jù),并以字符形式返回。
* @param Url: WebService服務(wù)地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務(wù)的命名空間,可以WSDL數(shù)據(jù)中找到 (http://***.com.cn/)
* @param MethodName: WebService的調(diào)用函數(shù)方法名稱(getDataMethod)
* @param Maps: 請(qǐng)求服務(wù)需要提交的數(shù)據(jù)集
* @Return: 服務(wù)以字符類型返回請(qǐng)求數(shù)據(jù)
* @Exception: 寫入控制臺(tái)日志
*/
public String getString(String Url, String NameSpace, String MethodName, Map RequestDatas) {
return getString(Url, NameSpace, MethodName, RequestDatas, null, null);
}
/*
* 獲取WebService數(shù)據(jù),并以字符形式返回。
* @param Url: WebService服務(wù)地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務(wù)的命名空間,可以WSDL數(shù)據(jù)中找到 (http://***.com.cn/)
* @param MethodName: WebService的調(diào)用函數(shù)方法名稱(getDataMethod)
* @param Maps: 請(qǐng)求服務(wù)需要提交的數(shù)據(jù)集
* @Return: 服務(wù)以字符類型返回請(qǐng)求數(shù)據(jù)
* @Exception: 寫入控制臺(tái)日志
*/
public String getString(String Url, String NameSpace, String MethodName) {
return getString(Url, NameSpace, MethodName, null, null, null);
}
/*
* 獲取WebService數(shù)據(jù),并以字符形式返回。
* @param Url: WebService服務(wù)地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務(wù)的命名空間,可以WSDL數(shù)據(jù)中找到 (http://***.com.cn/)
* @param MethodName: WebService的調(diào)用函數(shù)方法名稱(getDataMethod)
* @param Maps: 請(qǐng)求服務(wù)需要提交的數(shù)據(jù)集
* @param SoapHeadeName: 設(shè)置WebService的HTTP頭名稱
* @param SoapHeadeValues: 設(shè)置 SoapHeade 的數(shù)據(jù)集
* @Return: 服務(wù)以字符類型返回請(qǐng)求數(shù)據(jù)
* @Exception: 寫入控制臺(tái)日志
*/
public String getString(String Url, String NameSpace, String MethodName, Map RequestDatas, String SoapHeadeName, Map SoapHeadeValues) {
SoapObject soap = getObject(Url, NameSpace, MethodName, RequestDatas, SoapHeadeName, SoapHeadeValues);
if (soap != null && soap.getPropertyCount() > 0) {
String getResultString = soap.getProperty(0).toString();
return getResultString;
}
return null;
}
/*
* 獲取WebService數(shù)據(jù),返回SoapObject對(duì)象。
* @param Url: WebService服務(wù)地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務(wù)的命名空間,可以WSDL數(shù)據(jù)中找到 (http://***.com.cn/)
* @param MethodName: WebService的調(diào)用函數(shù)方法名稱(getDataMethod)
* @param Maps: 請(qǐng)求服務(wù)需要提交的數(shù)據(jù)集
* @Return: 服務(wù)返回SoapObject對(duì)象
* @Exception: 寫入控制臺(tái)日志
*/
public SoapObject getObject(String Url, String NameSpace, String MethodName, Map RequestDatas) {
return getObject(Url, NameSpace, MethodName, RequestDatas, null, null);
}
/*
* 獲取WebService數(shù)據(jù),返回SoapObject對(duì)象。
* @param Url: WebService服務(wù)地址 (http://webservice.***.com.cn/WeatherWS.asmx)
* @param NameSpace: WebService的服務(wù)的命名空間,可以WSDL數(shù)據(jù)中找到 (http://***.com.cn/)
* @param MethodName: WebService的調(diào)用函數(shù)方法名稱(getDataMethod)
* @param Maps: 請(qǐng)求服務(wù)需要提交的數(shù)據(jù)集
* @param SoapHeadeName: 設(shè)置WebService的HTTP頭名稱
* @param SoapHeadeValues: 設(shè)置 SoapHeade 的數(shù)據(jù)集
* @Return: 服務(wù)返回SoapObject對(duì)象
* @Exception: 寫入控制臺(tái)日志
*/
public SoapObject getObject(String Url, String NameSpace, String MethodName, Map RequestDatas, String SoapHeadeName, Map SoapHeadeValues) {
try {
SoapObject soap = new SoapObject(NameSpace, MethodName);
// 設(shè)置WebService提交的數(shù)據(jù)集
if (RequestDatas != null && !RequestDatas.isEmpty()) {
for (Map.Entry entry : RequestDatas.entrySet()) {
soap.addProperty(entry.getKey(), entry.getValue());
}
}
// 設(shè)置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 entry : SoapHeadeValues.entrySet()) {
Element element = new Element().createElement(NameSpace, entry.getKey());
element.addChild(Node.TEXT, entry.getValue());
header[0].addChild(Node.ELEMENT, element);
}
}
// 初始化數(shù)據(jù)請(qǐng)求
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = _isdotnet;
if (header != null) envelope.headerOut = header;
envelope.bodyOut = soap;
envelope.setOutputSoapObject(soap);
// 發(fā)起Web請(qǐng)求
HttpTransportSE http = new HttpTransportSE(Url, _setHttpTimeOut);
http.debug = _isdebug;
http.call(NameSpace + MethodName, envelope);
// 獲取Web請(qǐng)求結(jié)果, 數(shù)據(jù)需要從 result.getProperty(0) 獲取
SoapObject result = (SoapObject) envelope.bodyIn;
return result;
} catch (Exception e) {
e.getMessage();
}
return null;
}
}
第一個(gè)坑:
SoapSerializationEnvelope 在設(shè)置dotNet的時(shí)候要注意
/*
* 設(shè)置當(dāng)前WebServices是否支持 .net 的WebServices;
* @param dotNetWebService: .net默認(rèn)true;java默認(rèn)是false
*/
文章中也有提到
第二個(gè)坑:
就是傳遞參數(shù)。在工具類中我們也看到了我把參數(shù)寫成了Map集合然后來完成填充。但是問題就出現(xiàn)在這里,我如果使用ArrayMap或者HashMap的話,有部分值傳遞到后臺(tái)會(huì)丟失,所以參數(shù)的Map需要是LinkedHashMap最好
回調(diào)部分就沒寫了,有興趣的朋友可以自己進(jìn)一步封裝
總結(jié)
以上是生活随笔為你收集整理的android wsdl封装,《android 解析WebService》的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中object是什么数据类型
- 下一篇: (十四)【RecSys 2016】Per