java获得天气预报-webservice

/**
 * 
 */
package com.test;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class WeatherReport {
	/** 
	 * 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市 
	 *  
	 * 编写者:王景辉 
	 *  
	 * @param city 
	 *            用户输入的城市名称 
	 * @return 客户将要发送给服务器的SOAP  请求 
	 */
	private static String getSoapRequest(String city) {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
		sb.append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
		sb.append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		sb.append("<soap:Body><getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">");
		sb.append("<theCityName>" + city);
		sb.append("</theCityName></getWeatherbyCityName>");
		sb.append("</soap:Body></soap:Envelope>");
		return sb.toString();
	}

	/** 
	 * 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流 
	 *  
	 * 编写者:王景辉 
	 *  
	 * @param city 
	 *            用户输入的城市名称 
	 * @return 服务器端返回的输入流,供客户端读取 
	 * @throws Exception 
	 */
	private static InputStream getSoapInputStream(String city) throws Exception {
		try {
			String soap = getSoapRequest(city);
			if (soap == null) {
				return null;
			}
			URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.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://WebXml.com.cn/getWeatherbyCityName");

			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;
		}
	}

	/** 
	 * 对服务器端返回的XML进行解析 
	 *  
	 * 编写者:王景辉 
	 *  
	 * @param city 
	 *            用户输入的城市名称 
	 * @return 字符串 用,分割 
	 */
	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("string");
			StringBuffer sb = new StringBuffer();
			for (int count = 0; count < nl.getLength(); count++) {
				Node n = nl.item(count);
				if (n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
					sb = new StringBuffer("#");
					break;
				}
				sb.append(n.getFirstChild().getNodeValue() + "#/n");
			}
			is.close();
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	//  /**  
	//   * 生成天气xml文件  
	//   * @param args  
	//   * @throws Exception  
	//   */  
	//  public static void createXml(InputStream is){  
	//      try {  
	//          InputStreamReader isr = new InputStreamReader(is,"utf-8");  
	//          BufferedReader in = new BufferedReader(isr);  
	//          String inputLine;  
	//          BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("result.xml")));  
	//          while ((inputLine = in.readLine()) != null){  
	//              bw.write(inputLine);  
	//              bw.newLine();  
	//          }       
	//          bw.close();  
	//          in.close();  
	//          isr.close();  
	//      } catch (Exception e) {  
	//          e.printStackTrace();  
	//      }  
	//  }  

	public static void main(String[] args) throws Exception {
		System.out.println(getWeather("广州"));
	}
}


http://blog.csdn.net/guijava/article/details/4439647


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值