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文件,内容如下:

<!--?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类,数据参考返回的数据,代码如下:

 

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类,实现航班时刻查询

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   


 


 

你可能感兴趣的:(android,android,android,Android开发,Android开发,航班查询)