随机获取国际国内航班3字码城市对的方法

思路:通过访问开放航班城市查询webservice获取城市对3字码

    /**

     * 利用免费航班城市webservice获取随机城市对

     */

    public List<String> randomCity(){

        String endpoint = "http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx?wsdl";

        String nameSpace = "http://WebXml.com.cn/";



        String operationName = "getDomesticCity";



        Service service = new Service();

        Call call;

        String FOCElementBody = null;

        List<String> citys = new ArrayList<String>();

        try {

            call = (Call) service.createCall();

            call.setUseSOAPAction(true);

            call.setSOAPActionURI(nameSpace + operationName);

            call.setTargetEndpointAddress(endpoint);



            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);

            call.setOperationName(operationName);

            Object res = (Object) call.invoke(new Object[] {});

            Schema schema = (Schema) res;

            MessageElement[] msgele = schema.get_any();

            FOCElementBody = msgele[1].getChildren().toString().replace("[", "").replace("]", "");

            

            StringReader stringReader = new StringReader(FOCElementBody);

            InputSource inputSource = new InputSource(stringReader);

            DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory

                    .newInstance();

            DocumentBuilder docBuilder = docBuilderFac.newDocumentBuilder();

            Document document = docBuilder.parse(inputSource);

            Node node = document.getDocumentElement();

            NodeList temp = node.getChildNodes();

            

            for(int i=0;i<temp.getLength();i++){

                NodeList tmp = temp.item(i).getChildNodes();

                citys.add(tmp.item(2).getTextContent());

            }

            

        } catch (ServiceException e) {

            e.printStackTrace();

        } catch (RemoteException e) {

            e.printStackTrace();

        } catch (ParserConfigurationException e) {

            e.printStackTrace();

        } catch (SAXException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        

        String citytemp1 = citys.get(rand.nextInt(citys.size()));

        String citytemp2 = null;

        do{

            citytemp2 = citys.get(rand.nextInt(citys.size()));

        }while(citytemp1==citytemp2);

        

        citys.clear();

        citys.add(citytemp1);

        citys.add(citytemp2);

        return citys;

    }

 

你可能感兴趣的:(方法)