zoukankan      html  css  js  c++  java
  • 7.Web Service 调用天气代码

    1.

     1 2500多个城市天气预报 WEB服务公用事业
     2 Endpoint:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
     3 Disco:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?disco
     4 WSDL:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
     5 
     6 代码如下:
     7 
     8 import java.rmi.RemoteException;
     9 
    10 import javax.xml.namespace.QName;
    11 import javax.xml.rpc.ServiceException;
    12 
    13 import org.apache.axis.client.Call;
    14 import org.apache.axis.client.Service;
    15 
    16 
    17 
    18 
    19 public class Weather {
    20     public static void main(String[] args) throws ServiceException,
    21             RemoteException {
    22         String cityCode = "北京";
    23         String userId = "";
    24         // http://WebXml.com.cn/是wsdl中definitions根节点的targetNamespace属性
    25         // webservice路径
    26         // 这里后面加不加 "?wsdl" 效果都一样的
    27         String endpoint = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
    28         String[] res = null;
    29 
    30         // 查询城市天气的接口方法名
    31         String operationName = "getWeather";
    32         // 定义service对象
    33         Service service = new Service();
    34         // 创建一个call对象
    35         Call call = (Call) service.createCall();
    36         // 设置目标地址,即webservice路径
    37         call.setTargetEndpointAddress(endpoint);
    38         // 设置操作名称,即方法名称
    39         call.setOperationName(new QName("http://WebXml.com.cn/", operationName));
    40         // 设置方法参数
    41         call.addParameter(new QName("http://WebXml.com.cn/", "theCityCode"),
    42                 org.apache.axis.encoding.XMLType.XSD_STRING,
    43                 javax.xml.rpc.ParameterMode.IN);
    44         call.addParameter(new QName("http://WebXml.com.cn/", "theUserID"),
    45                 org.apache.axis.encoding.XMLType.XSD_STRING,
    46                 javax.xml.rpc.ParameterMode.IN);
    47         // 设置返回值类型
    48         // 对于返回是字符串数组的返回类型只有这两种可行
    49 
    50         // call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_VECTOR);
    51         call.setReturnClass(java.lang.String[].class);
    52 
    53         call.setUseSOAPAction(true);
    54         call.setSOAPActionURI("http://WebXml.com.cn/" + "getWeather");
    55 
    56         res = (String[]) call.invoke(new Object[] { cityCode, userId });
    57 
    58         // 如果返回类型是org.apache.axis.encoding.XMLType.SOAP_VECTOR时用下面的转型接收
    59         // Vector v=(Vector) call.invoke(new Object[]{cityCode,userId});
    60         for (String str : res) {
    61             System.out.println(str);
    62         }
    63     }
    64 }
  • 相关阅读:
    Pyspark 提交任务遇到的问题
    Python的 figure参数和 subplot子图绘制
    Python的 plot函数和绘图参数设置
    Python的散点图绘制 scatter
    Python的random操作
    python的浅复制与深复制
    Python的itertools.product 方法
    python的关联图绘制 --- pyecharts
    DVWA——Brute Force暴力破解
    DVWA——简介
  • 原文地址:https://www.cnblogs.com/sharpest/p/7856046.html
Copyright © 2011-2022 走看看