• 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 }
  • 相关阅读:
    setUP和tearDown,setUpClass和tearDownClass
    执行代码提示:Ran 0 tests in 0.000s
    Python自动化测试执行用例
    Python+webdriver自动化脚本的封装调用
    SQL之INDEX
    SQL之WHERE,GROUP,HAVING,ORDER BY
    SQL之LIMIT
    python基础之字符编码、文件处理
    python基础之列表、元组、字典、布尔值、集合
    python基础之if条件判断、while循环及数据类型
  • 原文地址:https://www.cnblogs.com/sharpest/p/7856046.html
Copyright © 2020-2023  润新知