您的位置:首页 > 编程语言 > Java开发

用JAVA做一个天气预报webservice的客户端

2006-11-13 19:43 549 查看
最近做的项目都是同webService有关的,自然就要关心一下webservice方面的资源。

http://www.wopos.com/webservice/Weather.asmx?op=getWeather 是一个天气预报的webservice,从它的输出结果来看天气数据应该来自中央气象局的问天网。过这方面就不用再多关心了,我们关心的是怎样调用这个webservice。
首先登录www.wopos.com/webservice/Weather.asmx?op=getWeather。可以看到如下的SOAP信息:

请求:


POST /webservice/Weather.asmx http/1.1


Host: www.wopos.com


Content-Type: text/xml; charset=utf-8


Content-Length: length


SOAPAction: "http://tempuri.org/getWeather"




<?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>


<getWeather xmlns="http://tempuri.org/">


<mCity>string</mCity>


</getWeather>


</soap:Body>


</soap:Envelope>



XML部分全部复制下来创建一个XML文件(普通的文本文件也可以),为了以后编程方面,把


      ...


<mCity>string</mCity>


...

改成


      ...


  <mCity>${city}$</mCity>


...



以weathersoap.xml保存在以后生成的类的同一目录。
响应:


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="h ttp://www.w3.org/2001/XMLSchema" xmlns:soap="h ttp://schemas.xmlsoap.org/soap/envelope/">


<soap:Body>


<getWeatherResponse xmlns="h ttp://tempuri.org/">


<getWeatherResult>string</getWeatherResult>


</getWeatherResponse>


</soap:Body>


</soap:Envelope>



在后面对XML的解释要用到响应部分的XML描述。


package jaqcy.weatherreport.client;




import java.io.*;


import java.net.*;


import javax.xml.parsers.*;


import org.w3c.dom.*;




/** *//**


*


* @author jaqcy


*/


public class WeatherReport




...{


private static String getSoapRequest(String city)//city为要查询天气的城市名




...{


try




...{


Class cls=Object.class;


InputStreamReader isr=new InputStreamReader(cls.getResourceAsStream("/jaqcy/weatherreport/client/weathersoap.xml"));//读取存在weathersoap的SOAP信息


BufferedReader reader=new BufferedReader(isr);


String soap="";


String tmp;


while((tmp=reader.readLine())!=null)




...{


soap+=tmp;


}


reader.close();


isr.close();


return soap.replace("${city}$",city);//用传入的参数city替换原来的${city}$


}


catch (Exception ex)




...{


ex.printStackTrace();


return null;


}


}




/**//*


*返回InputStream是因为w3c DOM中Document的parse方法可


*以接受InputStream类型的参数,方面在下一步对XML的解释


*/


private static InputStream getSoapInputStream(String city)throws Exception




...{


try




...{


String soap=getSoapRequest(city);


if(soap==null)




...{


return null;


}


URL url=new URL("http://www.wopos.com/webservice/Weather.asmx");


URLConnection conn=url.openConnection();


conn.setUseCaches(false);


conn.setDoInput(true);


conn.setDoOutput(true);




conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));


conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");


conn.setRequestProperty("SOAPAction",""http://tempuri.org/getWeather"");




OutputStream os=conn.getOutputStream();


OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");


osw.write(soap);


osw.flush();


osw.close();




InputStream is=conn.getInputStream();


return is;


}


catch(Exception e)




...{


e.printStackTrace();


return null;


}


}




/**//*


*用W3C DOM对返回的XML进行解释


*


*/


public static String getWeather(String city)




...{


try




...{


Document doc;


DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();


dbf.setNamespaceAware(true);


DocumentBuilder db=dbf.newDocumentBuilder();


InputStream is=getSoapInputStream(city);


doc=db.parse(is);


NodeList nl=doc.getElementsByTagName("getWeatherResult");


Node n=nl.item(0);


String weather=n.getFirstChild().getNodeValue();


is.close();


return weather;


}


catch(Exception e)




...{


e.printStackTrace();


return null;


}


}


}



写个main方法检验一下结果:


public static void main(String[] args)throws Exception




...{


System.out.println(WeatherReport.getWeather("珠海"));


}



结果如下:

城市==珠海,日期==4.02-4.03,图1==http://weather.tq121.com.cn/images/a1.gif,图2==http://weather.tq121.com.cn/images/00.gif,天气==多云,温度==28℃~22℃,风==微风,紫外线==弱
结果是有点乱,不过只要把它分割开来也是很容易的事。
在自己的应用程序中加上天气预报的功能,对程序的竞争力也是有好处的,而且实现也相当的简单,何乐而不为呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: