1. 程式人生 > >webservice呼叫國內飛機航班時刻表 WEB 服務

webservice呼叫國內飛機航班時刻表 WEB 服務

昨天幫朋友寫的一個呼叫國內航班時刻表的測試程式,折騰了我好一會的時間,打算把它分享出來,共大家參考,其中wsdl:http://ws.webxml.com.cn/webservices/DomesticAirline.asmx?wsdl
這個是介面文件:http://ws.webxml.com.cn/webservices/DomesticAirline.asmx
,這個wsdl是用來生產java類,但是生成以後呼叫不成功,沒辦法,只好用了httpclient自己解析的xml文件
以下是程式碼部分:

實體類AirLineInfo .java


package cn.com.finished;

/**
 * @author
Administrator * */
public class AirLineInfo { // <Company>吉祥航空</Company> // <AirlineCode>HO1252</AirlineCode> // <StartDrome>北京首都國際機場</StartDrome> // <ArriveDrome>上海虹橋國際機場</ArriveDrome> // <StartTime>06:50</StartTime> // <ArriveTime>09:05</ArriveTime>
// <Mode>320</Mode> // <AirlineStop>0</AirlineStop> // <Week>123456日</Week> // 返回資料:DataSet,Table(0)結構為 // Item(Company)航空公司、 // Item(AirlineCode)航班號、 // Item(StartDrome)出發機場、 // Item(ArriveDrome)到達機場、 // Item(StartTime)出發時間、 // Item(ArriveTime)到達時間、 // Item(Mode)機型、
// Item(AirlineStop)經停 // 、Item(Week)飛行週期(星期) private String company; private String airlineCode; private String startDrome; private String arriveDrome; private String startTime; private String arriveTime; private String mode; private String airlineStop; private String week; public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getAirlineCode() { return airlineCode; } public void setAirlineCode(String airlineCode) { this.airlineCode = airlineCode; } public String getStartDrome() { return startDrome; } public void setStartDrome(String startDrome) { this.startDrome = startDrome; } public String getArriveDrome() { return arriveDrome; } public void setArriveDrome(String arriveDrome) { this.arriveDrome = arriveDrome; } public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getArriveTime() { return arriveTime; } public void setArriveTime(String arriveTime) { this.arriveTime = arriveTime; } public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } public String getAirlineStop() { return airlineStop; } public void setAirlineStop(String airlineStop) { this.airlineStop = airlineStop; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } @Override public String toString() { return "AirLineInfo [航空公司=" + company + ", 航班號=" + airlineCode + ", 出發機場=" + startDrome + ", 到達機場=" + arriveDrome + ", 出發時間=" + startTime + ", 到達時間=" + arriveTime + ", 機型=" + mode + ", 經停=" + airlineStop + ", 飛行週期(星期)=" + week + "]"; } }

實體類CityInfo.java

package cn.com.finished;

public class CityInfo {
    private String enCityName;      //  (enCityName)城市英文名稱、
    private String cnCityName;      //  Item(cnCityName)城市中文名稱
    private String abbreviation;     // Item(Abbreviation)縮寫
    public String getEnCityName() {
        return enCityName;
    }
    public void setEnCityName(String enCityName) {
        this.enCityName = enCityName;
    }
    public String getCnCityName() {
        return cnCityName;
    }
    public void setCnCityName(String cnCityName) {
        this.cnCityName = cnCityName;
    }
    public String getAbbreviation() {
        return abbreviation;
    }
    public void setAbbreviation(String abbreviation) {
        this.abbreviation = abbreviation;
    }
    @Override
    public String toString() {
        return "CityInfo [城市英文名稱=" + enCityName + ", 城市中文名稱=" + cnCityName + ", 縮寫=" + abbreviation
                + "]";
    }

}

呼叫getDomesticAirlinesTime方法

public static List<AirLineInfo> getDomesticAirlinesTimeString(String startCity ,String lastCity,String  theDate,String userID) throws Exception{       
                String url = "/webservices/DomesticAirline.asmx/getDomesticAirlinesTime";       
                String host = "www.webxml.com.cn";    
                String param = "startCity="+URLEncoder.encode(startCity, "utf-8")+"&lastCity="+URLEncoder.encode(lastCity, "utf-8")+"&theDate="+theDate+"+&userID="+userID+"";    
                HttpClient httpclient = new DefaultHttpClient();    
                httpclient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET,"UTF-8");    

                HttpGet httpget = new HttpGet("http://"+host+url+"?"+param);    

                ResponseHandler<String> responseHandler = new BasicResponseHandler();    
                String responseBody = httpclient.execute(httpget, responseHandler); 
//              System.out.println(responseBody);    
                List<AirLineInfo> airList = getDomesticAirlinesTime(responseBody);
                httpclient.getConnectionManager().shutdown();
                return airList;

      } 
    //解析xml字串
        /**
         * 
         * @param xmlString 需要解析的xml字串,也即xml中的字串
         * @return
         */
        @SuppressWarnings("all")
        private static List<AirLineInfo>  getDomesticAirlinesTime (String xmlString) {
                // 使用 SAXReader 解析 XML   
                SAXReader saxReader = new SAXReader();  
                Document document = null;  
                try {  
                    document =  DocumentHelper.parseText(xmlString);
                } catch (DocumentException e) {  
                    e.printStackTrace();  
                }  

                // 獲取根節點  
                Element root = document.getRootElement();  
                // 列印節點名稱  
        //        System.out.println("<" + root.getName() + ">"); 

                // 獲取根節點下的子節點遍歷  
                Iterator<?> iter = root.elementIterator("diffgram");  
                //建立一個list集合用來儲存獲取到的資訊
                List<AirLineInfo> list=new ArrayList<AirLineInfo>();
                // 遍歷diffgram節點  
                while (iter.hasNext()) {  
                    // 獲取第一個diffgram節點  
                    Element empEle = (Element) iter.next(); 
        //           System.out.println("<" + empEle.getName() + ">"); 
                   //獲取Airlines節點
                    Iterator elementIterator = empEle.elementIterator("Airlines");
                    while (elementIterator.hasNext()) {
                        Element object = (Element) elementIterator.next();
                        Iterator airlinesTimes = object.elementIterator("AirlinesTime");
                        //建立AirLineInfo物件
                        AirLineInfo air=null;
                        while (airlinesTimes.hasNext()) {
                            air=new AirLineInfo();
                            //迴圈遍歷出了每一個AirlinesTime元素
                            Element airlinesTime = (Element) airlinesTimes.next();
//                          System.out.println(airlinesTime.getName());
                            Iterator everyElement = airlinesTime.elementIterator();
                            int count=1;
                            while (everyElement.hasNext()) {
                                Element every = (Element) everyElement.next();
//                              Item(Company)航空公司、
//                              Item(AirlineCode)航班號、
//                              Item(StartDrome)出發機場、
//                              Item(ArriveDrome)到達機場、
//                              Item(StartTime)出發時間、
//                              Item(ArriveTime)到達時間、
//                              Item(Mode)機型、
//                              Item(AirlineStop)經停
//                              、Item(Week)飛行週期(星期)
                                if(count==1){
                                    air.setCompany(every.getStringValue());
                                }else if (count==2) {
                                    air.setAirlineCode(every.getStringValue());
                                }else if (count==3) {
                                    air.setStartDrome(every.getStringValue());
                                }else if (count==4) {
                                    air.setArriveDrome(every.getStringValue());
                                }else if (count==5) {
                                    air.setStartTime(every.getStringValue());
                                }else if (count==6) {
                                    air.setArriveTime(every.getStringValue());
                                }else if (count==7) {
                                    air.setMode(every.getStringValue());
                                }else if (count==8) {
                                    air.setAirlineStop(every.getStringValue());
                                }else if (count==9) {
                                    air.setWeek(every.getStringValue());
                                }
//                              System.out.println(every.getStringValue());
                                count++;
                            }
                        //把物件新增到集合中
                            list.add(air);
                            //保證count為 1~6
                                count=1;
                        }
                    }   
                }
                //迴圈遍歷list集合
//              for (AirLineInfo airLineInfo : list) {
//                  System.out.println(airLineInfo);
//              }
                return list;
        }

呼叫getDomesticCity 方法

        @SuppressWarnings("all")
        public static List<CityInfo> getDomesticCityString () throws Exception{ 
                String url = "/webservices/DomesticAirline.asmx/getDomesticCity";       
                String host = "www.webxml.com.cn"; 

                @SuppressWarnings("deprecation")
                HttpClient httpclient = new DefaultHttpClient();    
                HttpGet httpget = new HttpGet("http://"+host+url+"");    
                ResponseHandler<String> responseHandler = new BasicResponseHandler();    
                String responseBody = httpclient.execute(httpget, responseHandler);
                List<CityInfo> cityList = getDomesticCity(responseBody);
//              System.out.println(responseBody);    
                httpclient.getConnectionManager().shutdown();
                return cityList;


        }
        @SuppressWarnings("all")
        private static List<CityInfo>  getDomesticCity(String xmlString) {
            // 使用 SAXReader 解析 XML   
            SAXReader saxReader = new SAXReader();  
            Document document = null;  
            try {  
                document =  DocumentHelper.parseText(xmlString);
            } catch (DocumentException e) {  
                e.printStackTrace();  
            }  

            // 獲取根節點  
            Element root = document.getRootElement();  
            // 列印節點名稱  
    //        System.out.println("<" + root.getName() + ">"); 

            // 獲取根節點下的子節點遍歷  
            Iterator<?> iter = root.elementIterator("diffgram");  
            //建立一個list集合用來儲存獲取到的資訊
            Map<String, AirLineInfo> map=new HashMap<String, AirLineInfo>();
            List<CityInfo> list=new ArrayList<CityInfo>();
            // 遍歷diffgram節點  
            while (iter.hasNext()) {  
                // 獲取第一個diffgram節點  
                Element empEle = (Element) iter.next(); 
    //           System.out.println("<" + empEle.getName() + ">"); 
               //獲取Airlines節點
                Iterator elementIterator = empEle.elementIterator("Airline1");
                while (elementIterator.hasNext()) {
                    Element object = (Element) elementIterator.next();
                    Iterator airlinesTimes = object.elementIterator("Address");
                    //建立CityInfo物件
                    CityInfo city=null;
                    while (airlinesTimes.hasNext()) {
                        city=new CityInfo();
                        //迴圈遍歷出了每一個AirlinesTime元素
                        Element airlinesTime = (Element) airlinesTimes.next();
//                      System.out.println(airlinesTime.getName());
                        Iterator everyElement = airlinesTime.elementIterator();
                        int count=1;
                        while (everyElement.hasNext()) {
                            Element every = (Element) everyElement.next();
                            if(count==1){
                                city.setEnCityName(every.getStringValue());
                            }else if (count==2) {
                                city.setCnCityName(every.getStringValue());
                            }else if (count==3) {
                                city.setAbbreviation(every.getStringValue());
                            }
//                          System.out.println(every.getStringValue());
                            count++;
                        }
                    //把物件新增到集合中
                        list.add(city);
                        //保證count為 1~3
                            count=1;
                    }
                }   
            }
            return list;
    }

測試類TestAir.java:

    List<CityInfo> cityList = DomesticAirline.getDomesticCityString();
        for (CityInfo cityInfo : cityList) {
            System.out.println(cityInfo);
        }
        List<AirLineInfo> list = DomesticAirline.getDomesticAirlinesTimeString("北京", "太原", "2016-10-2", "");
        for (AirLineInfo airLineInfo : list) {
            System.out.println(airLineInfo);
        }

至此呼叫完成,上一個成功的截圖
這裡寫圖片描述
這是eclipse專案以及jar包下載地址:webservice呼叫國內飛機航班時刻表下載連結
ok,完活!