当前位置: 代码迷 >> Web前端 >> java调用WebService天气预报范例1.0版
  详细解决方案

java调用WebService天气预报范例1.0版

热度:514   发布时间:2012-10-09 10:21:45.0
java调用WebService天气预报实例1.0版

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;

/**@src  http://eric-619.iteye.com/blog/693673*/
public class WeatherUtil {
	private static String SERVICES_HOST = "www.webxml.com.cn";
    private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
    private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL 
                                              + "getRegionProvince";
    private static String CITY_CODE_URL = WEATHER_SERVICES_URL
                                          + "getSupportCityString?theRegionCode=";
    private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
                                              + "getWeather?theUserID=&theCityCode=";
    
    
    private WeatherUtil(){}
    
    public static void main(String[] args) throws Exception{	
    	int provinceCode = getProvinceCode("山东");    //3119
    	int cityCode = getCityCode(provinceCode, "菏泽");    //974
    	List<String> weatherList = getWeather(cityCode);
        for(String weather:weatherList){
           System.out.println(weather);
        }
    }
    
    public static int getProvinceCode(String provinceName){	
        Document document;
        DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
        documentBF.setNamespaceAware(true);
        int provinceCode = 0;
        try{
			DocumentBuilder documentB = documentBF.newDocumentBuilder();
			InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL);    //具体webService相关
			document = documentB.parse(inputStream);
			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;
    }
    
    public static int getCityCode(int provinceCode, String cityName){	
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        int cityCode = 0;
        try{
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode);    //具体webService相关
			doc = db.parse(is);
			NodeList nl = doc.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);
				}
			}
			is.close();
		}catch(DOMException e){
			e.printStackTrace();
		}catch(ParserConfigurationException e){
			e.printStackTrace();
		}catch (SAXException e){
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
		return cityCode;
    }
    
    public static InputStream getSoapInputStream(String url){
        InputStream inputStream = null;
        try{
			URL urlObj = new 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;
    }
    
    public static List<String> getWeather(int cityCode){
        List<String> weatherList = new ArrayList<String>();
        Document document;
        DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
        documentBF.setNamespaceAware(true);
        try{
			DocumentBuilder documentB = documentBF.newDocumentBuilder();
			InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL + cityCode);
			document = documentB.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;
    }
    
}


1 楼 kongshanxuelin 2010-06-18  
千万不要用webxml,那个web service有流量和调用次数限制的,应该用google的天气预报
2 楼 fejay 2010-06-19  
来点注释撒
3 楼 清晨阳光 2010-06-20  
这个就是找个能用的WebService的地址罢了
4 楼 phenom 2010-06-20  
就是解析XML没有别的了?
5 楼 rainerliu 2010-06-24  
google的接口在哪里找得到啊!
楼主的也写的不错!一个简单的客户端,谢谢分享。
6 楼 javaliver 2010-10-04  
phenom 写道
就是解析XML没有别的了?

同感
7 楼 quxiaoyong 2010-10-06  
曾经我发了一个代码贴,跟楼主这个差不多,被各位看官残忍地投了隐藏贴。楼主要吸取教训呀。。
8 楼 zhanzhan02 2011-01-02  
javaliver 写道
phenom 写道
就是解析XML没有别的了?

同感

同感。并且好麻烦。
  相关解决方案