赛派号

音响插主机后面识别不到了 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文件,内容如下:

[html] view plain copy print ?       startCityTmp lastCityTmp

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

[ja] view plain copy print ? 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方法  }  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类,实现航班时刻查询

[ja] view plain copy print ? 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;  }  /* * string string string string */  private static String parseSOAP(InputStream xml) throws Exception {  // List  List 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();  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);  }  }  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; } /* * string string string string */ private static String parseSOAP(InputStream xml) throws Exception { // List List 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(); 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); } }

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了