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 }
  • 相关阅读:
    Java 的字符串,String、StringBuffer、StringBuilder 有什么区别?
    访问一个HTTPS的网站的大致流程
    Spring Boot Mybatis-Plus
    Spring Boot REST
    Spring Boot 以 war 方式部署
    MVC 框架
    HDFS,MongoDB,HBase的区别和使用场景
    什么时候该用NoSQL?
    最近和朋友微信卖螃蟹有点偏离重心了
    我要出去~
  • 原文地址:https://www.cnblogs.com/sharpest/p/7856046.html
Copyright © 2011-2022 走看看