==一下是以调用天气预报为例======
package ch09.server;
//产生随机数的接口
public interface Iweather {
public int getRandom();
}
//接口的实现类
package ch09.server;
import java.util.Random;
public class WeatherImpl implements Iweather {
//实现随机数接口的类 返回1-15的随机数
public int getRandom() {
Random rand=new Random();
int radom=rand.nextInt(16);//产生1~16的整数nextInt(100)时返回0-99
return radom;
}
}
1.选择要操作的项目
2点击菜单MyEclipase
3.选中子菜单第一个子菜单
4.在跳出的子子菜单中选中add XFire 然后单击下一步,
勾起XFire1.2Core..和XFire1.2HTTPClient.. 最后单击完成
5.会项目里自动生成WebServices文件夹,里面有一个services.xml配置文件,
web.xml文件也自动添加些配置
然后在services.xml手动添加
<service>
<name>weatherService</name><!--服务名称-->
<namespace>www.jbaptech.com.cn/AddressBook</namespace><!--命名空间(可有可无)名字自己定-->
<serviceClass>ch09.server.Iweather</serviceClass><!--服务的接口-->
<implementationClass>ch09.server.WeatherImpl</implementationClass><!--接口的实现类 -->
<scope>application</scope><!--范围 -->
</service>
6.测试
//创建天气预报web服务的地址(这里写的是本机)
/*(http://localhost:8080/restranthgk/services/weatherService?wsdl)利用此地址测试是否成功
* services/*为web.xml配置文件拦截下来访问的路径 weatherService为 services.xml里面name元素的值
* 路径对应正确才能成功 每次更改配置文件时都要重启tomcat 否则还是运行原来的代码
*/
在浏览器输入:
http://localhost:8080/restranthgk/services/weatherService?wsdl
//如果成功则显示服务信息
=====以上代表添加XFire库成功=====
=====下面我们写客户端代码=====
package ch09.client;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import ch09.server.Iweather;
//天气的客户端类
/*
*@auther heguikun-2010-9-29
*/
public class WeatherClient {
public int ramGain() {
int result=-1;//代表运行本方法出错时的默认值
//创建天气预报web服务的元数据
//实例化对象服务工厂并调用它的Crete方法传入参数(接口类) 获得服务的元数据
Service srvcModel=new ObjectServiceFactory().create(Iweather.class);//括号里面获得接口(类)
//创建天气预报web服务的代理
//实例化XFire的代理工厂XFireProxyFactory(XFireFactory.newInstance()获得xFire库的实例,getXFire()通过内部方法获得库)
XFireProxyFactory factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//创建天气预报web服务的地址(这里写的是本机)
/*(http://localhost:8080/restranthgk/services/weatherService?wsdl)利用此地址测试是否成功
* services/*为web.xml配置文件拦截下来访问的路径 weatherService为 services.xml里面的名
* 路径对应正确才能成功 每次更改配置文件时都要重启tomcat 否则还是运行原来的代码
*/
String helloWorldURL="http://localhost:8080/restranthgk/services/weatherService";
try {
//生成天气预报web服务调用对象
//(强制把object类型转换为接口类型)通过代理工厂factory.create(元数据,服务URL)
Iweather iClient=(Iweather)factory.create(srvcModel,helloWorldURL);
//获得0-15的随机整数
result=iClient.getRandom();//通过接口调用方法获得随机数
} catch (Exception e) {
}
return result;//返回
}
//显示天气最终的状况
public String showWeather() {
int weatherNum=this.ramGain();//调用上面的方法
String todayWeather="";
switch (weatherNum) {
case 0:
todayWeather="晴";
break;
case 1:
todayWeather="晴到多云";
break;
case 2:
todayWeather="多云";
break;
case 3:
todayWeather="阵雨";
break;
case 4:
todayWeather="中雨";
break;
case 5:
todayWeather="大雨";
break;
case 6:
todayWeather="大到暴雨";
break;
case 7:
todayWeather="雷阵雨";
break;
case 8:
todayWeather="雨夹雪";
break;
case 9:
todayWeather="小雪";
break;
case 10:
todayWeather="中雪";
break;
case 11:
todayWeather="大到暴雪";
break;
case 12:
todayWeather="冰雹";
break;
case 13:
todayWeather="霜冻";
break;
case 14:
todayWeather="雾";
break;
case 15:
todayWeather="阴";
break;
default:
todayWeather="预测不到,可能因为调用天气的API出错了";
break;
}
return todayWeather;
}
}
《《《《《最后在jsp页面调用
今天的天气:<%
String weatherString="错误";
try{
WeatherClient weather=new WeatherClient() ;
weatherString=weather.showWeather();
}
catch(Exception e)
{}
%><%=weatherString %>
//======成功便在页面显示今天的天气========