閱讀連接:Retrofit 用Soap協(xié)議訪問WebService 詳解
參考
1、java發(fā)HTTP POST請求(內(nèi)容為xml格式)
2、 android解析XML總結(jié)(SAX、Pull诵棵、Dom三種方式)
3弧关、Android利用Soap讀取WebService并且解析XML的DataSet數(shù)據(jù)
前言
1、首先不要把這個想的太復(fù)雜髓废,它就是使用【soap】協(xié)議的請求,數(shù)據(jù)格式都是【xml】该抒,基礎(chǔ)還是http的post請求瓦哎,但是它的規(guī)范顯然更多一些,總體逃不過【Request和Response】柔逼。
2蒋譬、以下所有的范例都是使用 【 WeatherWebService 】 這個網(wǎng)站,它提供了【Soap1.1 和 Soap1.2 】的請求范例愉适,有【Request和Response】報文可看犯助,這樣更好理解規(guī)范和格式
注意點:
1、Soap1.1 维咸、Soap1.2 :不同版本協(xié)議剂买,代表的header和xml都略有不同
2、Baseurl癌蓖、Header(Content-type瞬哼、SOAPAction)、RequestBody(Xml)租副、ResponseBody(Xml)
3坐慰、RequestBody(Xml):Envelope,NameSpace用僧、Body结胀、Method赞咙、Param
3、ResponseBody(Xml):Envelope糟港,NameSpace攀操、Body、Method秸抚、Param
WebService 基礎(chǔ)與注意點
舉例:天氣網(wǎng)站-獲得某省份下所有城市
Soap1.1:
1速和、xmlns后基本都是namespace,比如envelopse標(biāo)簽有三個namespace剥汤,getSupportCity這個方法名有一個namespace
2健芭、區(qū)分soap1.1的是:【xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"】
3、soap1.1的請求header有:【Content-Type: text/xml; charset=utf-8 】和【SOAPAction: "http://WebXml.com.cn/getSupportCity"】
//-------------------------------------Request------------------------------------
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8 //header中的哦~~
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getSupportCity" //header中的哦~~
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> //標(biāo)記為soap1.1協(xié)議
<soap:Body>
<getSupportCity xmlns="http://WebXml.com.cn/"> //method和其namespace
<byProvinceName>string</byProvinceName> //param
</getSupportCity>
</soap:Body>
</soap:Envelope>
//-------------------------------------Response------------------------------------
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getSupportCityResponse xmlns="http://WebXml.com.cn/"> //結(jié)果集啦~~
<getSupportCityResult>
<string>string</string>
<string>string</string>
</getSupportCityResult>
</getSupportCityResponse>
</soap:Body>
</soap:Envelope>
Soap1.2:
1秀姐、略,同上
2若贮、區(qū)分soap1.2的是:【xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"】
3省有、soap1.2的請求header有:【application/soap+xml; charset=utf-8 】和沒有【SOAPAction】
//-------------------------------------Requeset------------------------------------
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8 //header中的,與soap1.1不同哦谴麦,而且沒有soapaction了蠢沿,需要注意~~~~
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> //標(biāo)記為soap1.2協(xié)議
<soap12:Body>
<getSupportCity xmlns="http://WebXml.com.cn/">
<byProvinceName>string</byProvinceName>
</getSupportCity>
</soap12:Body>
</soap12:Envelope>
//-------------------------------------Response------------------------------------
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getSupportCityResponse xmlns="http://WebXml.com.cn/"> //結(jié)果集~~~
<getSupportCityResult>
<string>string</string>
<string>string</string>
</getSupportCityResult>
</getSupportCityResponse>
</soap12:Body>
</soap12:Envelope>