java调用天气接口实例

package com.xzm.util.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.springframework.util.StringUtils;
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;

/**
 * 天气预报接口
 * 
 * @author xzm
 * 
 */
public class UserServiceClient {

	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 WEATHER_QUERY_URL = WEATHER_SERVICES_URL
			+ "getWeather?theUserID=&theCityCode=";

	/**
	 * 城市代码 / 镇江: 1954
	 */
	private static String CITICODE = "青州";

	public static void main(String[] args) throws Exception {
		String desc = new UserServiceClient().getWeatherStr();
		
		System.out.println(desc);
	}

	public 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 String getWeatherStr() {
		String desc = "";
		try {
			List<String> weatherList = getWeather(CITICODE);
			if(!StringUtils.isEmpty(weatherList)&&weatherList.size()>7){
				StringBuffer buffer = new StringBuffer("您查询的地区是:");
				buffer.append(weatherList.get(0)).append(weatherList.get(1)).append(" 
当前时间:").append(weatherList.get(3)).append("
")
				.append(weatherList.get(4)).append("
").append(weatherList.get(5));
				desc=buffer.toString();
			}else{
				desc = "哎呦,我脑袋笨,没查到。。";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return desc;
		}
		return desc;
	}

	public List<String> getWeather(String 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();
				System.out.println(weather);
				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;
	}
}
原文地址:https://www.cnblogs.com/xuzhenmin/p/3578851.html