描写叙述:本接口主要是依据城市名称 + 线路名称 模糊查找城市公交线路信息。
当中cityName = URLEncoder.encode(cityName,"utf-8")
line = URLEncoder.encode(line,"utf-8")
代码实现例如以下:
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 buslist = getBusInfo(cityName,line);
// 存储文本信息
StringBuffer news = new StringBuffer();
if (buslist != null && buslist.size() > 0) {
for(int i=0;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 getBusInfo(String cityName,String line){
URL url = null;
List list = new ArrayList();
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
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
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());
}
}
}
}