首先、新建Web Project、并在该项目的根目录下新建build.xml
[xhtml] view plain copy print ?
- <?xmlversion="1.0"encoding="UTF-8"?>
- <project name="xfireAnt"basedir="."default="createClientCode">
- <propertyname="xfirelib"value="${basedir}/WebRoot/WEB-INF/lib"/>
- <propertyname="sources"value="${basedir}/src"/>
- <pathid="classpath">
- <filesetdir="${xfirelib}">
- <includename="*.jar"/>
- </fileset>
- </path>
-
- <targetname="createClientCode">
- <taskdefname="wsgen"classname="org.codehaus.xfire.gen.WsGenTask"classpathref="classpath"/>
- <wsgenoutputDirectory="${sources}"wsdl="http://www.ayandy.com/Service.asmx?wsdl"package="com.jadyer.client"overwrite="true"/>
- </target>
- </project>
<?xml version="1.0" encoding="UTF-8"?> <project name="xfireAnt" basedir="." default="createClientCode"> <property name="xfirelib" value="${basedir}/WebRoot/WEB-INF/lib"/> <property name="sources" value="${basedir}/src"/> <path id="classpath"> <fileset dir="${xfirelib}"> <include name="*.jar"/> </fileset> </path> <target name="createClientCode"> <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="classpath"/> <wsgen outputDirectory="${sources}" wsdl="http://www.ayandy.com/Service.asmx?wsdl" package="com.jadyer.client" overwrite="true"/> </target> </project>
然后拷贝以下的Jar包到该项目的//WebRoot//WEB-INF//lib//中
[java] view plain copy print ?
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/** * @see -------------------------------------------------------------- * @see 我们新建的build.xml主要用来生成客户端代码,所以就需要依赖以下的Jar包 * @see 以下的Jar包,除ant-1.8.1.jar外,均取自xfire-distribution-1.2.6.zip * @see ant-1.8.1.jar * @see activation-1.1.jar * @see commons-logging-1.0.4.jar * @see jaxb-api-2.0.jar * @see jaxb-impl-2.0.1.jar * @see jaxb-xjc-2.0.1.jar * @see jdom-1.0.jar * @see stax-api-1.0.1.jar * @see wsdl4j-1.6.1.jar * @see wstx-asl-3.2.0.jar * @see xbean-2.2.0.jar * @see xfire-all-1.2.6.jar * @see xfire-jsr181-api-1.0-M1.jar * @see XmlSchema-1.1.jar * @see -------------------------------------------------------------- * @see 我们自己所要编写的客户端调用类,在执行时,需要用到以下的两个Jar包 * @see 这两个Jar包,同样取自xfire-distribution-1.2.6.zip * @see commons-codec-1.3.jar * @see commons-httpclient-3.0.jar * @see -------------------------------------------------------------- */
然后我们执行build.xml文件,即Run As——Ant Build
待控制台提示BUILD SUCCESSFUL时,刷新当前Web项目
就会发现,Ant已经成功帮我们生成了客户端代码
接下来,我们再编写一个类,调用客户端代码,实现天气查询功能
[java] view plain copy print ?
- package com.jadyer.test;
- import java.util.List;
- import org.tempuri.ArrayOfString;
- import org.tempuri.TheDayFlagEnum;
- import com.jadyer.client.ServiceClient;
-
-
- public class ClientFromAnt {
- public staticvoid main(String[] args) {
-
- long startTime = System.currentTimeMillis();
- System.out.println("开始时间: " + startTime);
-
-
- ServiceClient client = new ServiceClient();
- ArrayOfString weather = client.getServiceSoap().getWeatherbyCityName("哈尔滨", TheDayFlagEnum.TODAY);
- List<String> weatherList = weather.getString();
- for(Object obj:weatherList.toArray()){
- System.out.println(obj);
- }
-
-
- long endTime = System.currentTimeMillis();
- System.out.println("结束时间: " + endTime);
- System.out.println("耗费时间: " + (endTime - startTime));
- }
- }
package com.jadyer.test; import java.util.List; import org.tempuri.ArrayOfString; import org.tempuri.TheDayFlagEnum; import com.jadyer.client.ServiceClient; /** * 提供免费天气服务的网站: http://www.ayandy.com */ public class ClientFromAnt { public static void main(String[] args) { //输出程序执行的开始时间 long startTime = System.currentTimeMillis(); System.out.println("开始时间: " + startTime); //调用天气服务的核心代码 ServiceClient client = new ServiceClient(); ArrayOfString weather = client.getServiceSoap().getWeatherbyCityName("哈尔滨", TheDayFlagEnum.TODAY); List<String> weatherList = weather.getString(); for(Object obj:weatherList.toArray()){ System.out.println(obj); } //输出程序的执行时间 long endTime = System.currentTimeMillis(); System.out.println("结束时间: " + endTime); System.out.println("耗费时间: " + (endTime - startTime)); } }
最后控制台打印类似下面的输出,即实现了天气预报功能
[ruby] view plain copy print ?
- 开始时间: 1294654106437
- 2011-1-10 18:08:30 org.apache.commons.httpclient.HttpMethodBase writeRequest
- 信息: 100 (continue) read timeout. Resume sending the request
- null
- 哈尔滨
- 晴
- -16 ~ -25 ℃
- 微风
- 今天
- http://www.ayandy.com/images/晴.gif
- null
- 结束时间: 1294654111093
- 耗费时间: 4656
转载地址:http://blog.csdn.net/jadyer/article/details/6127517