六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 41|回复: 0

Java WebService之Axis学习(三):java调用天气webservice服务

[复制链接]

升级  2%

13

主题

13

主题

13

主题

秀才

Rank: 2

积分
53
 楼主| 发表于 2013-1-27 13:13:36 | 显示全部楼层 |阅读模式
    废话不多说,上次说道发布自己的webservice,这次我们来看看如何调用别人写的webservice,这是一个牛人写的代码,我对他进行了简单的分析,大家可以参考参考:
 

/** * @author :LYL *@date:2011-4-20,下午05:04:10 */package com.lyl.webservice;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.DOMException;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;public class WeatherClient {//服务主机private static String SERVICES_HOST = "www.webxml.com.cn";//天气服务URLprivate static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";//获取省份code的URLprivate static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL + "getRegionProvince";//获取城市code的URLprivate static String CITY_CODE_URL = WEATHER_SERVICES_URL+ "getSupportCityString?theRegionCode=";//天气查询的URLprivate static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL+ "getWeather?theUserID=&theCityCode=";private WeatherClient(){}public static void main(String[] args) throws Exception{ int provinceCode = getProvinceCode("江苏");    int cityCode = getCityCode(provinceCode, "苏州");   List<String> weatherList = getWeather(cityCode);for(String weather:weatherList){System.out.println(weather);}}/** * 得到省份ID * @param provinceName * @return */public static int getProvinceCode(String provinceName){ Document document;DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//设定支持命名空间dbf.setNamespaceAware(true);int provinceCode = 0;try{ //通过文档构建器工厂获取一个文档构建器DocumentBuilder db = dbf.newDocumentBuilder();//获得province_code的输入流InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL);    //具体webService相关document = db.parse(inputStream);//获取所有标签名为string的节点NodeList nodeList = document.getElementsByTagName("string");    //具体webService相关int len = nodeList.getLength();for(int i = 0; i < len; i++){Node n = nodeList.item(i);//获得省份名和对应的代码String result = n.getFirstChild().getNodeValue();//将省份名和代码区分开String[] address = result.split(",");String pName = address[0];String pCode = address[1];//判断是否是我们需要的省份if(pName.equalsIgnoreCase(provinceName)){provinceCode = Integer.parseInt(pCode);} }//关闭流inputStream.close();}catch(DOMException e){e.printStackTrace();}catch(ParserConfigurationException e){e.printStackTrace();}catch (SAXException e){ e.printStackTrace();}catch(IOException e){e.printStackTrace();}return provinceCode;}/** * 获取城市ID * @param provinceCode * @param cityName * @return */public static int getCityCode(int provinceCode, String cityName){ Document document;DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();dbf.setNamespaceAware(true);int cityCode = 0;try{DocumentBuilder db = dbf.newDocumentBuilder();InputStream inputStream = getSoapInputStream(CITY_CODE_URL + provinceCode);    //具体webService相关document = db.parse(inputStream);NodeList nl = document.getElementsByTagName("string");    //具体webService相关int len = nl.getLength();for(int i = 0; i < len; i++){Node n = nl.item(i);String result = n.getFirstChild().getNodeValue();String[] address = result.split(",");String cName = address[0];String cCode = address[1];if(cName.equalsIgnoreCase(cityName)){cityCode = Integer.parseInt(cCode);}}inputStream.close();}catch(DOMException e){e.printStackTrace();}catch(ParserConfigurationException e){e.printStackTrace();}catch (SAXException e){e.printStackTrace();}catch(IOException e) {e.printStackTrace();}return cityCode;}/** * 取得url连接返回的输入流 * @param url * @return */public static InputStream getSoapInputStream(String url){InputStream inputStream = null;try{URL urlObj = new URL(url);//得到URL所引用的远程对象的连接URLConnection urlConn = urlObj.openConnection();//设置一般请求属性urlConn.setRequestProperty("Host", SERVICES_HOST);    //具体webService相关urlConn.connect();//返回从此打开的连接读取的输入流inputStream = urlConn.getInputStream();}catch(MalformedURLException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}return inputStream;}/** * 获取天气信息 * @param cityCode * @return */public static List<String> getWeather(int cityCode){List<String> weatherList = new ArrayList<String>();Document document;DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();dbf.setNamespaceAware(true);try{DocumentBuilder db = dbf.newDocumentBuilder();InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL + cityCode);document = db.parse(inputStream);NodeList nl = document.getElementsByTagName("string");int len = nl.getLength();for(int i = 0; i < len; i++){Node n = nl.item(i);String weather = n.getFirstChild().getNodeValue();weatherList.add(weather);}inputStream.close();}catch(UnsupportedEncodingException e){e.printStackTrace();}catch (DOMException e){e.printStackTrace();}catch (ParserConfigurationException e){e.printStackTrace();}catch(SAXException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}return weatherList;}} 
大家如果想自己编写可以参考此代码,至于调用何种服务要跟具体的服务提供商有关,大家可以用此例子试验下。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表