昨天帮朋友写的一个调用国内航班时刻表的测试程序,折腾了我好一会的时间,打算把它分享出来,共大家参考,其中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 {
// 吉祥航空
// HO1252
// 北京首都国际机场
// 上海虹桥国际机场
// 06:50
// 09:05
// 320
// 0
// 123456日
// 返回数据: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 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 responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
// System.out.println(responseBody);
List airList = getDomesticAirlinesTime(responseBody);
httpclient.getConnectionManager().shutdown();
return airList;
}
//解析xml字符串
/**
*
* @param xmlString 需要解析的xml字符串,也即xml中的字符串
* @return
*/
@SuppressWarnings("all")
private static List 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 list=new ArrayList();
// 遍历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 cityList = DomesticAirline.getDomesticCityString();
for (CityInfo cityInfo : cityList) {
System.out.println(cityInfo);
}
List list = DomesticAirline.getDomesticAirlinesTimeString("北京", "太原", "2016-10-2", "");
for (AirLineInfo airLineInfo : list) {
System.out.println(airLineInfo);
}
至此调用完成,上一个成功的截图
这是eclipse项目以及jar包下载地址:webservice调用国内飞机航班时刻表下载链接
ok,完活!