1. 程式人生 > >公交線路免費api介面程式碼

公交線路免費api介面程式碼

package org.wx.xhelper.model;

/**
 * 公交資訊實體類
 * @author wangxw
 * @version 1.0
 * @date Jul 8, 2014 8:15:49 AM
 */
public class Bus {
	
	// 結果數量
	private int resultNum;
	
	// 線路名稱
	private String name;
	
	// 線路資訊
	private String info;
	
	// 沿途站點
	private String stats;
	
	// 途徑站點經緯度
	private String statXys;
	
	// 線路座標
	private String xys;
	
	// 描述
	private String description;

	public int getResultNum() {
		return resultNum;
	}

	public void setResultNum(int resultNum) {
		this.resultNum = resultNum;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getStats() {
		return stats;
	}

	public void setStats(String stats) {
		this.stats = stats;
	}

	public String getStatXys() {
		return statXys;
	}

	public void setStatXys(String statXys) {
		this.statXys = statXys;
	}

	public String getXys() {
		return xys;
	}

	public void setXys(String xys) {
		this.xys = xys;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
	
}

package org.wx.xhelper.service;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.wx.xhelper.model.Bus;

/**
 * 公交查詢服務介面類
 * @author wangxw
 * @version 1.0
 * @date Jul 2, 2014 1:55:14 PM
 */
public class BusQueryService {
	
	/**
	 * 生成公交查詢資訊
	 * @return 公交資訊 
	 * @throws UnsupportedEncodingException 
	 */
	public static String getBusInfoDetail(String cityName,String line) throws UnsupportedEncodingException{
		// 獲取線路資訊
		List<Bus> buslist = getBusInfo(cityName,line);
		
		// 儲存文字資訊
		StringBuffer news = new StringBuffer();
		
		if (buslist != null && buslist.size() > 0) {
			for(int i=0;i<buslist.size();i++){
				if(buslist.get(i).getName().startsWith(line)){
					if(news.length()==0){
						news.append(buslist.get(i).getDescription()).append("\n\n");
					}else{
						news.append(buslist.get(i).getDescription());
					}
				}
			}
		}
		
		if(news.length() == 0){
			news.append("沒有").append(line).append("這條線路,請輸入準確線路!");
		}
		
		return news.toString();
	}
	
	/**
	 * 獲取線路資訊
	 * @param cityName
	 * @param line
	 * @return
	 */
	public static List<Bus> getBusInfo(String cityName,String line){
		URL url = null;
		List<Bus> list = new ArrayList<Bus>();
		try{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder();
			
			try {
				cityName = URLEncoder.encode(cityName,"utf-8");
				line = URLEncoder.encode(line,"utf-8");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
			
			url = new URL("http://openapi.aibang.com/bus/lines?app_key=f41c8afccc586de03a99c86097e98ccb&city="+cityName+"&q="+line);
			
			Document doc = builder.parse(url.openStream());
			NodeList node = doc.getElementsByTagName("line"); 
			for(int i=0;i<node.getLength();i++){ 
				Bus bus = new Bus();
				String name = "";
				String info = "";
				String stats = "";
				String statXys = "";
				String xys = "";
				if(doc.getElementsByTagName("name").item(i).getFirstChild() != null){
					name = doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
				}
				if(doc.getElementsByTagName("info").item(i).getFirstChild() != null){
					info = doc.getElementsByTagName("info").item(i).getFirstChild().getNodeValue();
				}
				if(doc.getElementsByTagName("stats").item(i).getFirstChild() != null){
					stats = doc.getElementsByTagName("stats").item(i).getFirstChild().getNodeValue();
				}
				if(doc.getElementsByTagName("stat_xys").item(i).getFirstChild() != null){
					statXys = doc.getElementsByTagName("stat_xys").item(i).getFirstChild().getNodeValue();
				}
				if(doc.getElementsByTagName("xys").item(i).getFirstChild() != null){
					xys = doc.getElementsByTagName("xys").item(i).getFirstChild().getNodeValue();
				}
				bus.setName(name);
				bus.setInfo(info);
				bus.setStats(stats);
				bus.setStatXys(statXys);
				bus.setXys(xys);
				bus.setDescription(name + "\n" + info + "\n" + stats);
				list.add(bus);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
}

查詢線路結果優化:

      由於線路查詢使用模糊查詢,故查詢結果會有多個。

     比如 輸入 蘇州+2路 查詢結果會有:2路、夜2路、遊2路、快線2號、報關報檢專2線、軌道交通2號線等多個資訊。如果要做到精確查詢,現做以下改進:

     過濾掉路線不是以2路開頭的線路資訊

		// 儲存文字資訊
		StringBuffer news = new StringBuffer();
		
		if (buslist != null && buslist.size() > 0) {
			for(int i=0;i<buslist.size();i++){
				if(buslist.get(i).getName().startsWith(line)){
					if(news.length()==0){
						news.append(buslist.get(i).getDescription()).append("\n\n");
					}else{
						news.append(buslist.get(i).getDescription());
					}
				}
			}
		}