当前位置: 移动技术网 > IT编程>软件设计>架构 > webservice调用天气服务

webservice调用天气服务

2019年08月04日  | 移动技术网IT编程  | 我要评论

常见服务:http://www.webxml.com.cn/zh_cn/support.aspx

可能用到缺少的arrayofstring.java文件

package com.test.wes.weather;

import java.util.arraylist;
import java.util.list;
import javax.xml.bind.annotation.xmlaccesstype;
import javax.xml.bind.annotation.xmlaccessortype;
import javax.xml.bind.annotation.xmlelement;
import javax.xml.bind.annotation.xmltype;


/**
 * <p>arrayofstring complex type的 java 类。
 * 
 * <p>以下模式片段指定包含在此类中的预期内容。
 * 
 * <pre>
 * &lt;complextype name="arrayofstring">
 *   &lt;complexcontent>
 *     &lt;restriction base="{http://www.w3.org/2001/xmlschema}anytype">
 *       &lt;sequence>
 *         &lt;element name="string" type="{http://www.w3.org/2001/xmlschema}string" maxoccurs="unbounded" minoccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexcontent>
 * &lt;/complextype>
 * </pre>
 * 
 * 
 */
@xmlaccessortype(xmlaccesstype.field)
@xmltype(name = "arrayofstring", proporder = {
    "string"
})
public class arrayofstring {

    @xmlelement(nillable = true)
    protected list<string> string;

    /**
     * gets the value of the string property.
     * 
     * <p>
     * this accessor method returns a reference to the live list,
     * not a snapshot. therefore any modification you make to the
     * returned list will be present inside the jaxb object.
     * this is why there is not a <code>set</code> method for the string property.
     * 
     * <p>
     * for example, to add a new item, do as follows:
     * <pre>
     *    getstring().add(newitem);
     * </pre>
     * 
     * 
     * <p>
     * objects of the following type(s) are allowed in the list
     * {@link string }
     * 
     * 
     */
    public list<string> getstring() {
        if (string == null) {
            string = new arraylist<string>();
        }
        return this.string;
    }

}

wsimport用法

用法: wsimport [options] <wsdl_uri>

\其中 [options] 包括:
  -b <path>                 指定 jaxws/jaxb 绑定文件或附加模式
                            (每个 <path> 都必须具有自己的 -b)
  -b<jaxboption>            将此选项传递给 jaxb 模式编译器
  -catalog <file>           指定用于解析外部实体引用的目录文件
                            支持 tr9401, xcatalog 和 oasis xml 目录格式。
  -d <directory>            指定放置生成的输出文件的位置
  -encoding <encoding>      指定源文件所使用的字符编码
  -extension                允许供应商扩展 - 不按规范
                            指定功能。使用扩展可能会
                            导致应用程序不可移植或
                            无法与其他实现进行互操作
  -help                     显示帮助
  -httpproxy:<host>:<port>  指定 http 代理服务器 (端口默认为 8080)
  -keep                     保留生成的文件
  -p <pkg>                  指定目标程序包
  -quiet                    隐藏 wsimport 输出
  -s <directory>            指定放置生成的源文件的位置
  -target <version>         按给定的 jaxws 规范版本生成代码
                            默认为 2.2, 接受的值为 2.0, 2.1 和 2.2
                            例如, 2.0 将为 jaxws 2.0 规范生成兼容的代码
  -verbose                  有关编译器在执行什么操作的输出消息
  -version                  输出版本信息
  -wsdllocation <location>  @webserviceclient.wsdllocation 值
  -clientjar <jarfile>      创建生成的 artifact 的 jar 文件以及
                            调用 web 服务所需的 wsdl 元数据。
  -generatejws              生成存根 jws 实现文件
  -impldestdir <directory>  指定生成 jws 实现文件的位置
  -implservicename <name>   生成的 jws 实现的服务名的本地部分
  -implportname <name>      生成的 jws 实现的端口名的本地部分

\扩展:
  -xadditionalheaders              映射标头不绑定到请求或响应消息不绑定到
                                   java 方法参数
  -xauthfile                       用于传送以下格式的授权信息的文件:
                                   http://username:password@example.org/stock?wsdl
  -xdebug                          输出调试信息
  -xno-addressing-databinding      允许 w3c endpointreferencetype 到 java 的绑定
  -xnocompile                      不编译生成的 java 文件
  -xdisableauthenticator           禁用由 jax-ws ri 使用的验证程序,
                                   将忽略 -xauthfile 选项 (如果设置)
  -xdisablesslhostnameverification 在提取 wsdl 时禁用 ssl 主机名
                                   验证

\示例:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl

 


 

平时的用法

wsimport -encoding utf-8 -p com.test.wes.weather -s . http://ws.webxml.com.cn/webservices/weatherws.asmx?wsdl

先把文件下载下来weatherws.asmx

 再weatherws.asmx里

 <s:element ref="s:schema"/><s:any/>全部替换为​ <s:any minoccurs="2" maxoccurs="2"/>


 

wsimport -encoding utf-8 -p com.test.wes.weather -s . weatherws.asmx

 

运行上面的句子,生成了下面代码

 

 

 


package com.test.wec;

import java.io.ioexception;
import java.net.malformedurlexception;
import java.net.url;
import java.util.list;

import javax.xml.namespace.qname;
import javax.xml.ws.service;

import com.test.wes.phone.mobilecodewssoap;
import com.test.wes.weather.arrayofstring;
import com.test.wes.weather.weatherws;
import com.test.wes.weather.weatherwssoap;
 
 

public class serviceclient {
 
    public static void main(string[] args) throws ioexception {
        //创建wsdl的url,注意不是服务地址
//        url url = new url("http://ws.webxml.com.cn/webservices/weatherws.asmx?wsdl");
//        
//        //创建服务名称
//        //1.namespaceuri - 命名空间地址
//        //2.localpart - 服务视图名
//        qname qname = new qname("http://webxml.com.cn/", "qqonlinewebservice");
//        
//        //创建服务视图
//        //参数解释:
//        //1.wsdldocumentlocation - wsdl地址
//        //2.servicename - 服务名称
//        service service = service.create(url, qname);
//        //获取服务实现类
////        qqonlinewebservicesoap qqonlinesssoap =service.getport(qqonlinewebservicesoap.class);
////        //调用查询方法
////        string result = qqonlinesssoap.qqcheckonline("1565465");
////        system.out.println(result);
        
        weatherws factory = new weatherws();
        //根据工厂创建一个weatherwssoap对象
        weatherwssoap weatherwssoap = factory.getweatherwssoap();
        //调用webservice提供的getweather方法获取南宁市的天气预报情况
        arrayofstring weatherinfo = weatherwssoap.getsupportcitystring("广东");
        list<string> lstweatherinfo = weatherinfo.getstring();
        //遍历天气预报信息
        for (string s : lstweatherinfo) {
            system.out.print(s+"|");
        }
        system.out.println();
        
        arrayofstring weatherinfo1 = weatherwssoap.getweather("广州", "");
        list<string> lstweatherinfo1 = weatherinfo1.getstring();
        //遍历天气预报信息
        for (string s : lstweatherinfo1) {
            system.out.print(s+"|");
        }
        system.out.println();
        
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网