一、webService 是什么颗祝?
webservice 是一種跨編程語(yǔ)言 和跨操作系統(tǒng)平臺(tái)的遠(yuǎn)程調(diào)用技術(shù)著蛙。
也就是說(shuō),不管你是java桦山,還是.net 只要按照規(guī)范就可以進(jìn)行通信攒射。實(shí)現(xiàn)數(shù)據(jù)交互等。
這里說(shuō)的"規(guī)范"恒水,WebService平臺(tái)必須提供一套標(biāo)準(zhǔn)的類(lèi)型系統(tǒng)会放,用于溝通不同平臺(tái)、編程語(yǔ)言和組件模型中的不同類(lèi)型系統(tǒng)钉凌。Webservice平臺(tái)必須提供一種標(biāo)準(zhǔn)來(lái)描述Web service鸦概,讓客戶(hù)可以得到足夠的信息來(lái)調(diào)用這個(gè)Web service。最后甩骏,我們還必須有一種方法來(lái)對(duì)這個(gè)Web service進(jìn)行遠(yuǎn)程調(diào)用,這種方法實(shí)際是一種遠(yuǎn)程過(guò)程調(diào)用協(xié)議窗市。Webservice 就是一種部署在網(wǎng)絡(luò)上的API。
WebService是一種基于SOAP協(xié)議的遠(yuǎn)程調(diào)用標(biāo)準(zhǔn)饮笛,SOAP(Simple Object Access Protocol咨察,簡(jiǎn)單對(duì)象訪(fǎng)問(wèn)協(xié)議)是一種輕量級(jí)的、簡(jiǎn)單的福青、基于XML的協(xié)議摄狱,被設(shè)計(jì)用于在分布式環(huán)境中交換格式化和固化信息的簡(jiǎn)單協(xié)議脓诡。也就是說(shuō),要進(jìn)行通信媒役,進(jìn)行數(shù)據(jù)訪(fǎng)問(wèn)傳輸祝谚,就必須依賴(lài)于一定的協(xié)議,而SOAP正是WebService通信中所依賴(lài)的一種協(xié)議酣衷。通過(guò)webservice可以將不同操作系統(tǒng)平臺(tái)交惯、不同語(yǔ)言、不同技術(shù)整合到一塊穿仪。在Android SDK中并沒(méi)有提供調(diào)用WebService的庫(kù)席爽,因此,需要使用第三方的SDK來(lái)調(diào)用WebService啊片。適合手機(jī)的WebService客戶(hù)端的SDK有一些只锻,但目前我在開(kāi)發(fā)中遇到的是Ksoap2,并且搜索了一下紫谷,目前比較常用的是Ksoap2齐饮,其次我對(duì)Ksoap2不是很了解。所以目前只簡(jiǎn)單介紹Ksoap2的使用方法笤昨。
二祖驱、為什么使用webService?
Web Service主要是為了使原來(lái)各孤立的站點(diǎn)之間的信息能夠相互通信咬腋、共享而提出的一種接口羹膳。
Web Service所使用的是Internet上統(tǒng)一、開(kāi)放的標(biāo)準(zhǔn)根竿,如HTTP陵像、XML、SOAP(簡(jiǎn)單對(duì)象訪(fǎng)問(wèn)協(xié)議)寇壳、WSDL等醒颖,所以Web Service可以在任何支持這些標(biāo)準(zhǔn)的環(huán)境(Windows,Linux)中使用。
WebService遵循標(biāo)準(zhǔn)的SOAP協(xié)議壳炎,SOAP協(xié)議的內(nèi)容格式固定泞歉,SOAP協(xié)議傳遞的內(nèi)容是XML數(shù)據(jù),由于WebService是基于HTTP的匿辩,所以簡(jiǎn)單理解SOAP=HTTP+XML腰耙,因?yàn)閃ebService要經(jīng)過(guò)兩次序列化,所以導(dǎo)致其傳輸速度慢铲球,適用于沒(méi)有性能要求且數(shù)據(jù)傳輸量小的場(chǎng)景挺庞,推薦在公開(kāi)接口上使用WebService。
webservice離不開(kāi)web服務(wù)器(apache),web服務(wù)器是一個(gè)在公網(wǎng)上提供http通信的服務(wù)器.webservice 一般都建立在web服務(wù)器上,以供別人調(diào)用稼病。
WebService效率差點(diǎn)选侨,走h(yuǎn)ttp協(xié)議掖鱼,一般不會(huì)被防火墻屏蔽。
webservice援制,基于http協(xié)議戏挡,以xml為載體的通信方式。
好處:一直觀(guān)晨仑,二標(biāo)準(zhǔn)褐墅,也方便異構(gòu)系統(tǒng)的交互。
如果想提供對(duì)外的業(yè)務(wù)訪(fǎng)問(wèn)的接口寻歧,可能需要瀏覽器掌栅,各種語(yǔ)言的客戶(hù)端等各種異構(gòu)系統(tǒng)訪(fǎng)問(wèn)秩仆,是首選码泛。
如何使用WebService?
1澄耍、首先導(dǎo)入Ksoap2 jar包
2噪珊、通過(guò)webServiceUtil 中寫(xiě)一個(gè)獲取WebService數(shù)據(jù),返回SoapObject對(duì)象的方法齐莲,只需圖中的幾個(gè)參數(shù)痢站,下面的方法參數(shù)沒(méi)有用到的可以為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ù)方法名稱(chēng)(getDataMethod)
* @param Maps: 請(qǐng)求服務(wù)需要提交的數(shù)據(jù)集
* @param SoapHeadeName: 設(shè)置WebService的HTTP頭名稱(chēng)
* @param SoapHeadeValues: 設(shè)置 SoapHeade 的數(shù)據(jù)集
* @Return: 服務(wù)返回SoapObject對(duì)象
* @Exception: 寫(xiě)入控制臺(tái)日志
*/ public SoapObject GetObject(String Url, String NameSpace, String MethodName, Map<String, String> RequestDatas, String SoapHeadeName, Map<String, String> SoapHeadeValues) {
SoapObject result;
try {
SoapObject soap = new SoapObject(NameSpace, MethodName);
// 系統(tǒng)日志輸出
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);
StringBuffer requestParams = new StringBuffer();
// 設(shè)置WebService提交的數(shù)據(jù)集
if (RequestDatas != null && !RequestDatas.isEmpty()) {
for (Map.Entry<String, String> entry : RequestDatas.entrySet()) {
soap.addProperty(entry.getKey(), entry.getValue());
requestParams.append( entry.getKey() + "=" + entry.getValue() + " ");
}
}
if(_iswritelog) System.out.println("[SOAP Params] : "+ requestParams.toString());
// 設(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<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);
}
}
// 初始化數(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) 獲取
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());
result = null;
}
return result;
}
總結(jié):雖然寫(xiě)了這篇文章呜叫,但我對(duì)webService并不是非常的理解,目前只知道他的簡(jiǎn)單的用法殿衰,上網(wǎng)查了一下朱庆,目前用webService的Android項(xiàng)目很少,希望能幫助你們一點(diǎn)闷祥。