客户端,服务器,天气预报
引用:http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html
上一節中我們通過http協議,采用HttpClient向服務器端action請求數據。當然調用服務器端方法獲取數據并不止這一種。WebService也可以為我們提供所需數據,
那么什么是webService呢?,它是一種基于SAOP協議的遠程調用標準,通過webservice可以將不同操作系統平臺,不同語言,不同技術整合到一起。
? 我們在PC機器java客戶端中,需要一些庫,比如XFire,Axis2,CXF等等來支持訪問WebService,但是這些庫并不適合我們資源有限的android手機客戶端,做過JAVA ME的人都知道有KSOAP這個第三方的類庫,可以幫助我們獲取服務器端webService調用,當然KSOAP已經提供了基于android版本的jar包了,那么我們就開始吧:
首先下載KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包
然后新建android項目:并把下載的KSOAP包放在android項目的lib目錄下:右鍵->build path->configure build path--選擇Libraries,如圖:
以下分為七個步驟來調用WebService方法:
第一:實例化SoapObject 對象,指定webService的命名空間(從相關WSDL文檔中可以查看命名空間),以及調用方法名稱。如:
View Code //命名空間privatestaticfinalString serviceNameSpace="http://WebXml.com.cn/";
//調用方法(獲得支持的城市)
privatestaticfinalString getSupportCity="getSupportCity";
//實例化SoapObject對象
SoapObject request=newSoapObject(serviceNameSpace, getSupportCity);
第二步:假設方法有參數的話,設置調用方法參數
request.addProperty("參數名稱","參數值");
第三步:設置SOAP請求信息(參數部分為SOAP協議版本號,與你要調用的webService中版本號一致):
View Code //獲得序列化的EnvelopeSoapSerializationEnvelope envelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
第四步:注冊Envelope,
(new MarshalBase64()).register(envelope);
第五步:構建傳輸對象,并指明WSDL文檔URL:
View Code //請求URLprivatestaticfinalString serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//Android傳輸對象
AndroidHttpTransport transport=newAndroidHttpTransport(serviceURL);
transport.debug=true;
第六步:調用WebService(其中參數為1:命名空間+方法名稱,2:Envelope對象):
View Code第七步:解析返回數據:
View Code這樣就成功啦。那么現在我們就來測試下吧,這里有個地址提供webService天氣預報的服務的,我這里只提供獲取城市列表:
View Code?然后你可以在瀏覽器中輸入地址(WSDL):serviceURL,你會看到一些可供調用的方法:
?我們選擇獲取國內外主要城市或者省份的方法吧:getSupportProvice,然后調用,你會發現瀏覽器返回給我們的是xml文檔:
View Code我們可以用 listview來顯示:
那么下面我將給出全部代碼:
View Code以及幫助類:
View Code publicclassWebServiceUtil {//命名空間
privatestaticfinalString serviceNameSpace="http://WebXml.com.cn/";
//請求URL
privatestaticfinalString serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//調用方法(獲得支持的城市)
privatestaticfinalString getSupportCity="getSupportCity";
//調用城市的方法(需要帶參數)
privatestaticfinalString getWeatherbyCityName="getWeatherbyCityName";
//調用省或者直轄市的方法(獲得支持的省份或直轄市)
privatestaticfinalString getSupportProvince="getSupportProvince";
/*************
* @return城市列表
*************/
publicstaticList<String>getCityList(){
//實例化SoapObject對象
SoapObject request=newSoapObject(serviceNameSpace, getSupportCity);
//獲得序列化的Envelope
SoapSerializationEnvelope envelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
(newMarshalBase64()).register(envelope);
//Android傳輸對象
AndroidHttpTransport transport=newAndroidHttpTransport(serviceURL);
transport.debug=true;
//調用
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(){
//實例化SoapObject對象
SoapObject request=newSoapObject(serviceNameSpace, getSupportProvince);
//獲得序列化的Envelope
SoapSerializationEnvelope envelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
(newMarshalBase64()).register(envelope);
//Android傳輸對象
AndroidHttpTransport transport=newAndroidHttpTransport(serviceURL);
transport.debug=true;
//調用
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);
}
}
以上就是我所作的查詢天氣預報的全部核心代碼了,讀者可以根據注釋以及本文章了解下具體實現,相信很快就搞明白了,運行結果如下:
?到此結束,下一節主要是socket通信了。
總結
以上是生活随笔為你收集整理的客户端,服务器,天气预报的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新浪微博注册用户超3亿 六成活跃者使用移
- 下一篇: XenServer中Windows 7与