web服務的調用案例

Web服務的調用
author:何桂坤

==一下是以調用天氣預報爲例======
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 %>

//======成功便在頁面顯示今天的天氣========

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章