zoukankan      html  css  js  c++  java
  • 学习Webservice之入天气小试

    主要方法是:通过程序中设置代理用公司内网访问外部Webservice

    public InputStream getSoapInputStream(String url) {
    InputStream inputStream = null;
    try {
    URL urlObj = new URL(url);
    InetSocketAddress addr = new InetSocketAddress("dailifuwuqiIP", duankou);//此处为代理服务器地址,端口
    Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
    HttpURLConnection urlConn = (HttpURLConnection) urlObj
    .openConnection(proxy);
    urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
    urlConn.setRequestMethod("GET");
    urlConn.connect();
    inputStream = urlConn.getInputStream();
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return inputStream;
    }

    类文件:

    Weather.java
    
    package weather;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.MalformedURLException;
    import java.net.Proxy;
    import java.net.URL;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    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 weather {
    // private static String SERVICES_HOST = "www.webxml.com.cn";
    private static String SERVICES_HOST = "webservice.webxml.com.cn";
    
    /**
    * 城市代码 / 浦东: 2015
    */
    private static int CITICODE = 2015;
    
    public static void main(String[] args) throws Exception {
    String time=null;
    Calendar cal=Calendar.getInstance();
    SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");//MM要大写
    time=format.format(cal.getTime());
    
    String desc = "今天是" + time+",浦东";
    desc += new weather().getWeatherStr();
    System.out.println(desc);
    }
    
    /**
    * Sos,MMyTest
    * */
    public InputStream getSoapInputStream(String url) {
    InputStream inputStream = null;
    try {
    URL urlObj = new URL(url);
    InetSocketAddress addr = new InetSocketAddress("XXX.XX.XX.XX", XXXX);//此处为代理服务器,和端口
    Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
    HttpURLConnection urlConn = (HttpURLConnection) urlObj
    .openConnection(proxy);
    urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
    urlConn.setRequestMethod("GET");
    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 (weatherList != null && weatherList.size() > 7) {
    String tianqi = weatherList.get(7);
    if (tianqi.contains("")) {
    tianqi = tianqi.substring(tianqi.indexOf("") + 1);
    }
    String wendu = weatherList.get(8);
    desc += ",天气" + tianqi;
    desc += " ,";
    desc += wendu.replace("", "").replace("/", "--");
    }
    } catch (Exception e) {
    e.printStackTrace();
    return desc;
    }
    return desc;
    }
    
    public 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("http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theUserID=&theCityCode=2015");
    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;
    }
    }

    运行weather.java文件后打印出:

    今天是2014-04-30,浦东,天气 晴转多云 ,14度--24度

  • 相关阅读:
    linux -- 部署java服务器(3) linux安装redis
    linux 安装php8
    linux mysql查看日志
    linux mysql常用的命令
    perl heredoc
    perl数值进制
    提问的智慧
    How to ask question the smart way
    PERL命令行
    图灵/异步图书
  • 原文地址:https://www.cnblogs.com/shoubianxingchen/p/3700954.html
Copyright © 2011-2022 走看看