当前位置: 代码迷 >> Web前端 >> Java WebService之Axis学习(3):java调用天气webservice服务
  详细解决方案

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

热度:585   发布时间:2012-09-18 16:21:42.0
Java WebService之Axis学习(三):java调用天气webservice服务

? ? 废话不多说,上次说道发布自己的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";
	//天气服务URL
	private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
	//获取省份code的URL
	private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL 
	+ "getRegionProvince";
	//获取城市code的URL
	private static String CITY_CODE_URL = WEATHER_SERVICES_URL
	+ "getSupportCityString?theRegionCode=";
	//天气查询的URL
	private 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;
	}

}

?

大家如果想自己编写可以参考此代码,至于调用何种服务要跟具体的服务提供商有关,大家可以用此例子试验下。

  相关解决方案