java调用webservice获取指定城市天气情况的例子

来源:互联网 发布:苹果手机屏幕修复软件 编辑:程序博客网 时间:2024/05/23 19:14
 

package test;

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

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 TRun {
 //method 要调用的webservie方法名,paramName请求参数名,paramValue请求参数值
 private static String makeSoapRequest(String method,String paramName,String paramValue) {
  StringBuffer sb = new StringBuffer();
  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/\">");
  // 构造请求内容,这里需要将XXX替换为webservice的地址
  sb.append("<soap:Body><"+method+" xmlns=\"http://WebXml.com.cn/\"><"+paramName+">" + paramValue + "</"+paramName+"></"+method+"></soap:Body>");
  sb.append("</soap:Envelope>");
  return sb.toString();
 }

 private static InputStream getSoapInputStream(String method,String paramName,String paramValue) throws Exception {
  try {
   String soap = makeSoapRequest(method,paramName,paramValue);
   if (soap == null) {
    return null;
   }
   // 需要替换成webservice的完整地址
   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;
  }
 }
 
 public static String getWeather(String city) {   
        try {   
            Document doc;   
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();   
            dbf.setNamespaceAware(true);   
            DocumentBuilder db = dbf.newDocumentBuilder();   
            InputStream is = getSoapInputStream("getWeatherbyCityName","theCityName",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;   
        }   
    } 

 public static void main(String[] args) throws ParseException {
  //参数为要查询的planid
  System.out.println(getWeather("武汉"));   
 }

}