客户端,服务器,天气预报
引用:http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html
上一節(jié)中我們通過(guò)http協(xié)議,采用HttpClient向服務(wù)器端action請(qǐng)求數(shù)據(jù)。當(dāng)然調(diào)用服務(wù)器端方法獲取數(shù)據(jù)并不止這一種。WebService也可以為我們提供所需數(shù)據(jù),
那么什么是webService呢?,它是一種基于SAOP協(xié)議的遠(yuǎn)程調(diào)用標(biāo)準(zhǔn),通過(guò)webservice可以將不同操作系統(tǒng)平臺(tái),不同語(yǔ)言,不同技術(shù)整合到一起。
? 我們?cè)赑C機(jī)器java客戶端中,需要一些庫(kù),比如XFire,Axis2,CXF等等來(lái)支持訪問(wèn)WebService,但是這些庫(kù)并不適合我們資源有限的android手機(jī)客戶端,做過(guò)JAVA ME的人都知道有KSOAP這個(gè)第三方的類庫(kù),可以幫助我們獲取服務(wù)器端webService調(diào)用,當(dāng)然KSOAP已經(jīng)提供了基于android版本的jar包了,那么我們就開(kāi)始吧:
首先下載KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包
然后新建android項(xiàng)目:并把下載的KSOAP包放在android項(xiàng)目的lib目錄下:右鍵->build path->configure build path--選擇Libraries,如圖:
以下分為七個(gè)步驟來(lái)調(diào)用WebService方法:
第一:實(shí)例化SoapObject 對(duì)象,指定webService的命名空間(從相關(guān)WSDL文檔中可以查看命名空間),以及調(diào)用方法名稱。如:
View Code //命名空間privatestaticfinalString serviceNameSpace="http://WebXml.com.cn/";
//調(diào)用方法(獲得支持的城市)
privatestaticfinalString getSupportCity="getSupportCity";
//實(shí)例化SoapObject對(duì)象
SoapObject request=newSoapObject(serviceNameSpace, getSupportCity);
第二步:假設(shè)方法有參數(shù)的話,設(shè)置調(diào)用方法參數(shù)
request.addProperty("參數(shù)名稱","參數(shù)值");
第三步:設(shè)置SOAP請(qǐng)求信息(參數(shù)部分為SOAP協(xié)議版本號(hào),與你要調(diào)用的webService中版本號(hào)一致):
View Code //獲得序列化的EnvelopeSoapSerializationEnvelope envelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
第四步:注冊(cè)Envelope,
(new MarshalBase64()).register(envelope);
第五步:構(gòu)建傳輸對(duì)象,并指明WSDL文檔URL:
View Code //請(qǐng)求URLprivatestaticfinalString serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//Android傳輸對(duì)象
AndroidHttpTransport transport=newAndroidHttpTransport(serviceURL);
transport.debug=true;
第六步:調(diào)用WebService(其中參數(shù)為1:命名空間+方法名稱,2:Envelope對(duì)象):
View Code第七步:解析返回?cái)?shù)據(jù):
View Code這樣就成功啦。那么現(xiàn)在我們就來(lái)測(cè)試下吧,這里有個(gè)地址提供webService天氣預(yù)報(bào)的服務(wù)的,我這里只提供獲取城市列表:
View Code?然后你可以在瀏覽器中輸入地址(WSDL):serviceURL,你會(huì)看到一些可供調(diào)用的方法:
?我們選擇獲取國(guó)內(nèi)外主要城市或者省份的方法吧:getSupportProvice,然后調(diào)用,你會(huì)發(fā)現(xiàn)瀏覽器返回給我們的是xml文檔:
View Code我們可以用 listview來(lái)顯示:
那么下面我將給出全部代碼:
View Code以及幫助類:
View Code publicclassWebServiceUtil {//命名空間
privatestaticfinalString serviceNameSpace="http://WebXml.com.cn/";
//請(qǐng)求URL
privatestaticfinalString serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//調(diào)用方法(獲得支持的城市)
privatestaticfinalString getSupportCity="getSupportCity";
//調(diào)用城市的方法(需要帶參數(shù))
privatestaticfinalString getWeatherbyCityName="getWeatherbyCityName";
//調(diào)用省或者直轄市的方法(獲得支持的省份或直轄市)
privatestaticfinalString getSupportProvince="getSupportProvince";
/*************
* @return城市列表
*************/
publicstaticList<String>getCityList(){
//實(shí)例化SoapObject對(duì)象
SoapObject request=newSoapObject(serviceNameSpace, getSupportCity);
//獲得序列化的Envelope
SoapSerializationEnvelope envelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
(newMarshalBase64()).register(envelope);
//Android傳輸對(duì)象
AndroidHttpTransport transport=newAndroidHttpTransport(serviceURL);
transport.debug=true;
//調(diào)用
try{
transport.call(serviceNameSpace+getWeatherbyCityName, envelope);
if(envelope.getResponse()!=null){
returnparse(envelope.bodyIn.toString());
}
} catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch(XmlPullParserException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnnull;
}
publicstaticList<String>getProviceList(){
//實(shí)例化SoapObject對(duì)象
SoapObject request=newSoapObject(serviceNameSpace, getSupportProvince);
//獲得序列化的Envelope
SoapSerializationEnvelope envelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
(newMarshalBase64()).register(envelope);
//Android傳輸對(duì)象
AndroidHttpTransport transport=newAndroidHttpTransport(serviceURL);
transport.debug=true;
//調(diào)用
try{
transport.call(serviceNameSpace+getWeatherbyCityName, envelope);
if(envelope.getResponse()!=null){
returnnull;
}
} catch(IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch(XmlPullParserException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
returnnull;
}
/*************
* @paramcityName
* @return
*************/
publicstaticString getWeather(String cityName){
return"";
}
/**************
* 解析XML
* @paramstr
* @return
*/
privatestaticList<String>parse(String str){
String temp;
List<String>list=newArrayList<String>();
if(str!=null&&str.length()>0){
intstart=str.indexOf("string");
intend=str.lastIndexOf(";");
temp=str.substring(start, end-3);
String []test=temp.split(";");
for(inti=0;i<test.length;i++){
if(i==0){
temp=test[i].substring(7);
}else{
temp=test[i].substring(8);
}
intindex=temp.indexOf(",");
list.add(temp.substring(0, index));
}
}
returnlist;
}
/*********
* 獲取天氣
* @paramsoapObject
*/
privatevoidparseWeather(SoapObject soapObject){
//String date=soapObject.getProperty(6);
}
}
以上就是我所作的查詢天氣預(yù)報(bào)的全部核心代碼了,讀者可以根據(jù)注釋以及本文章了解下具體實(shí)現(xiàn),相信很快就搞明白了,運(yùn)行結(jié)果如下:
?到此結(jié)束,下一節(jié)主要是socket通信了。
總結(jié)
以上是生活随笔為你收集整理的客户端,服务器,天气预报的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 新浪微博注册用户超3亿 六成活跃者使用移
- 下一篇: XenServer中Windows 7与