Android利用接口实现航班时刻查询

http://www.chenwg.com/technology/android/59-flight-search.html

在网上发现一个接口,是关于航班查询的,http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx ,而这又与web service的知识有关,所以就写一个简单的例子。

看看这个接口文档,返回的数据是xml格式的,所以我们要将返回的xml格式的数据进行解析,解析我选择Android自带的pull解析。

创建一个Android工程,在src下新建一个flight.xml文件,内容如下:

  1. <!--?xml version="1.0" encoding="utf-8"?--> 
  2. <soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
  3. <soap:body> 
  4. <?XML:NAMESPACE PREFIX = [default] http://WebXml.com.cn/ NS = "http://WebXml.com.cn/" /><getdomesticairlinestime xmlns="http://WebXml.com.cn/"> 
  5. <startcity>startCityTmp</startcity> 
  6. <lastcity>lastCityTmp</lastcity> 
  7. <thedate></thedate> 
  8. <userid></userid> 
  9. </getdomesticairlinestime> 
  10. </soap:body> 
  11. </soap:envelope> 
<!--?xml version="1.0" encoding="utf-8"?-->
<soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:body>
<?XML:NAMESPACE PREFIX = [default] http://WebXml.com.cn/ NS = "http://WebXml.com.cn/" /><getdomesticairlinestime xmlns="http://WebXml.com.cn/">
<startcity>startCityTmp</startcity>
<lastcity>lastCityTmp</lastcity>
<thedate></thedate>
<userid></userid>
</getdomesticairlinestime>
</soap:body>
</soap:envelope>

新建一个Flight类,数据参考返回的数据,代码如下:

  1. public class Flight { 
  2. private String company; 
  3. private String airlineCode; 
  4. private String startDrome; 
  5. private String arriveDrome; 
  6. private String startTime; 
  7. private String arriveTime; 
  8. private String mode; 
  9. private String airlineStop; 
  10. private String week; 
  11. //省略getter和setter方法 
public class Flight {
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;
//省略getter和setter方法
}


创建FlightsService类,实现航班时刻查询.创建FlightsService类,实现航班时刻查询.创建FlightsService类,实现航班时刻查询

  1. public class FlightsService { 
  2. /**
  3. * 获取航班時刻表
  4. *
  5. * @param startCity
  6. * 出發城市
  7. * @param lastCity
  8. * 到達城市
  9. * @return
  10. * @throws Exception
  11. */ 
  12. public static String getFlight(String startCity, String lastCity) 
  13. throws Exception { 
  14. //设置网络代理,如果你不是使用代理上网,就将这两行注释掉即可 
  15. System.setProperty("http.proxyHost", "10.123.74.137"); 
  16. System.setProperty("http.proxyPort", "808"); 
  17. String soap = readSoap(); 
  18. soap = soap.replaceAll("startCityTmp", startCity); 
  19. soap = soap.replaceAll("lastCityTmp", lastCity); 
  20. byte[] entity = soap.getBytes(); 
  21. String path = "http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx"
  22. HttpURLConnection conn = (HttpURLConnection) new URL(path) 
  23. .openConnection(); 
  24. conn.setConnectTimeout(5000); 
  25. conn.setRequestMethod("POST"); 
  26. conn.setDoOutput(true); 
  27. conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
  28. conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); 
  29. conn.getOutputStream().write(entity); 
  30. if (conn.getResponseCode() == 200) { 
  31. return parseSOAP(conn.getInputStream()); 
  32. return null
  33. /*
  34. * <!--?xml version="1.0" encoding="utf-8"?-->
  35. <soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  36. <soap:body>
  37. <?XML:NAMESPACE PREFIX = [default] http://WebXml.com.cn/ NS = "http://WebXml.com.cn/" /><getdomesticairlinestime xmlns="http://WebXml.com.cn/">
  38. <startcity>string</startcity>
  39. <lastcity>string</lastcity>
  40. <thedate>string</thedate>
  41. <userid>string</userid>
  42. </getdomesticairlinestime>
  43. </soap:body>
  44. </soap:envelope>
  45. */ 
  46. private static String parseSOAP(InputStream xml) throws Exception { 
  47. // List 
  48. List<flight> flightList = null
  49. Flight flight = null
  50. //使用Android提供的工具类创建pullParser解析器 
  51. XmlPullParser pullParser = Xml.newPullParser(); 
  52. pullParser.setInput(xml, "UTF-8"); 
  53. //触发第一个事件 
  54. int event = pullParser.getEventType(); 
  55. //解析xml,文档没结束就一直处理 
  56. while (event != XmlPullParser.END_DOCUMENT) { 
  57. switch (event) { 
  58. case XmlPullParser.START_DOCUMENT: 
  59. //初始化flightlist 
  60. flightList = new ArrayList<flight>(); 
  61. break
  62. case XmlPullParser.START_TAG: 
  63. //遇到要处理的标签就处理 
  64. if ("AirlinesTime".equals(pullParser.getName())) { 
  65. flight = new Flight(); 
  66. } else if ("Company".equals(pullParser.getName())) { 
  67. flight.setCompany(pullParser.nextText()); 
  68. } else if ("AirlineCode".equals(pullParser.getName())) { 
  69. flight.setAirlineCode(pullParser.nextText()); 
  70. } else if ("StartDrome".equals(pullParser.getName())) { 
  71. flight.setStartDrome(pullParser.nextText()); 
  72. } else if ("ArriveDrome".equals(pullParser.getName())) { 
  73. flight.setArriveDrome(pullParser.nextText()); 
  74. } else if ("StartTime".equals(pullParser.getName())) { 
  75. flight.setStartTime(pullParser.nextText()); 
  76. } else if ("ArriveTime".equals(pullParser.getName())) { 
  77. flight.setArriveTime(pullParser.nextText()); 
  78. } else if ("Mode".equals(pullParser.getName())) { 
  79. flight.setMode(pullParser.nextText()); 
  80. } else if ("AirlineStop".equals(pullParser.getName())) { 
  81. flight.setAirlineStop(pullParser.nextText()); 
  82. } else if ("Week".equals(pullParser.getName())) { 
  83. flight.setWeek(pullParser.nextText()); 
  84. break
  85. case XmlPullParser.END_TAG: 
  86. if ("AirlinesTime".equals(pullParser.getName())) { 
  87. flightList.add(flight); 
  88. flight = null
  89. break
  90. //用next方法处理下一个事件,不然就成死循环了 
  91. event = pullParser.next(); 
  92. //在Logcat打印出来,看结果 
  93. for (int i = 0; i < flightList.size(); i++) { 
  94. Log.v("AirlineCode", flightList.get(i).getAirlineCode()); 
  95. return null
  96. private static String readSoap() throws Exception { 
  97. InputStream inStream = FlightsService.class.getClassLoader() 
  98. .getResourceAsStream("flight.xml"); 
  99. byte[] data = StreamTool.read(inStream); 
  100. return new String(data); 
  101. }</flight></flight> 
public class FlightsService {
/**
* 获取航班時刻表
* 
* @param startCity
* 出發城市
* @param lastCity
* 到達城市
* @return
* @throws Exception
*/
public static String getFlight(String startCity, String lastCity)
throws Exception {
//设置网络代理,如果你不是使用代理上网,就将这两行注释掉即可
System.setProperty("http.proxyHost", "10.123.74.137");
System.setProperty("http.proxyPort", "808");
String soap = readSoap();
soap = soap.replaceAll("startCityTmp", startCity);
soap = soap.replaceAll("lastCityTmp", lastCity);
byte[] entity = soap.getBytes();
String path = "http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx";
HttpURLConnection conn = (HttpURLConnection) new URL(path)
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
conn.getOutputStream().write(entity);
if (conn.getResponseCode() == 200) {
return parseSOAP(conn.getInputStream());
}
return null;
}
/*
* <!--?xml version="1.0" encoding="utf-8"?-->
<soap:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:body>
<?XML:NAMESPACE PREFIX = [default] http://WebXml.com.cn/ NS = "http://WebXml.com.cn/" /><getdomesticairlinestime xmlns="http://WebXml.com.cn/">
<startcity>string</startcity>
<lastcity>string</lastcity>
<thedate>string</thedate>
<userid>string</userid>
</getdomesticairlinestime>
</soap:body>
</soap:envelope>
*/
private static String parseSOAP(InputStream xml) throws Exception {
// List
List<flight> flightList = null;
Flight flight = null;
//使用Android提供的工具类创建pullParser解析器
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(xml, "UTF-8");
//触发第一个事件
int event = pullParser.getEventType();
//解析xml,文档没结束就一直处理
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
//初始化flightlist
flightList = new ArrayList<flight>();
break;
case XmlPullParser.START_TAG:
//遇到要处理的标签就处理
if ("AirlinesTime".equals(pullParser.getName())) {
flight = new Flight();
} else if ("Company".equals(pullParser.getName())) {
flight.setCompany(pullParser.nextText());
} else if ("AirlineCode".equals(pullParser.getName())) {
flight.setAirlineCode(pullParser.nextText());
} else if ("StartDrome".equals(pullParser.getName())) {
flight.setStartDrome(pullParser.nextText());
} else if ("ArriveDrome".equals(pullParser.getName())) {
flight.setArriveDrome(pullParser.nextText());
} else if ("StartTime".equals(pullParser.getName())) {
flight.setStartTime(pullParser.nextText());
} else if ("ArriveTime".equals(pullParser.getName())) {
flight.setArriveTime(pullParser.nextText());
} else if ("Mode".equals(pullParser.getName())) {
flight.setMode(pullParser.nextText());
} else if ("AirlineStop".equals(pullParser.getName())) {
flight.setAirlineStop(pullParser.nextText());
} else if ("Week".equals(pullParser.getName())) {
flight.setWeek(pullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if ("AirlinesTime".equals(pullParser.getName())) {
flightList.add(flight);
flight = null;
}
break;
}
//用next方法处理下一个事件,不然就成死循环了
event = pullParser.next();
}
//在Logcat打印出来,看结果
for (int i = 0; i < flightList.size(); i++) {
Log.v("AirlineCode", flightList.get(i).getAirlineCode());
}
return null;
}
private static String readSoap() throws Exception {
InputStream inStream = FlightsService.class.getClassLoader()
.getResourceAsStream("flight.xml");
byte[] data = StreamTool.read(inStream);
return new String(data);
}
}</flight></flight>


源码我也放上来了,下载吧!

http://pan.baidu.com/share/link?shareid=197530&uk=1796216265  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值