描写叙述:本接口主要是依据城市名称 +  线路名称 模糊查找城市公交线路信息。

开源api接口:http://openapi.aibang.com/bus/lines?app_key=keyvalue&city="+cityName+"&q="+line

当中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<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());
}
}
}
}

公交线路免费api接口代码的更多相关文章

  1. 身份证归属地查询免费api接口代码

    描写叙述 :依据身份证编号 查询归属地信息. 身份证实体类: package org.wx.xhelper.model; /** * 身份证实体类 * @author wangxw * @versio ...

  2. 手机号码归属地查询免费api接口代码

    依据手机号码查询用户的卡类型.运营商.归属地.区域等信息. 手机归属地实体类 package org.wx.xhelper.model; /** * 手机归属地 * @author wangxw * ...

  3. 违章查询免费api接口代码

    能够依据城市+车牌号+发动机号查询违章信息列表. 违章实体类 package org.wx.xhelper.model; /** * 违章实体类 * @author wangxw * @version ...

  4. 免费API 接口罗列,再也不愁没有服务器开发不了APP了(下)【申明:来源于网络】

    免费API 接口罗列,再也不愁没有服务器开发不了APP了(下)[申明:来源于网络] 地址:http://mp.weixin.qq.com/s/QzZTIG-LHlGOrzfdvCVR1g

  5. 各类无次数限制的免费API接口整理

    各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...

  6. 网络免费API接口整理

    转载自: https://www.cnblogs.com/doit8791/p/9351629.html 从网上看到一些免费API接口,在个人开发小程序等应用练手时可试用. 各类无次数限制的免费API ...

  7. 各类无次数限制的免费API接口,再也不怕找不到免费API了

    各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...

  8. 免费API接口记录

    用来记录一些无次数限制的免费API接口,主要是聚合数据上和API Store上的一些,还有一些其他的. 手机号码归属地API接口: https://www.juhe.cn/docs/api/id/11 ...

  9. 快递单号查询免费api接口(PHP示例)

    快递单号查询API,可以对接顺丰快递查询,邮政快递查询,中通快递查询等.这些快递物流企业,提供了快递单号自动识别接口,快递单号查询接口等快递物流服务.对于电商企业,ERP服务企业,集成此接口到自己的软 ...

随机推荐

  1. The reference to entity "characterEncoding" must end with the ';' delimiter

    数据源配置时加上编码转换格式后出问题了: The reference to entity "characterEncoding" must end with the ';' del ...

  2. HDoj-1527-取石子游戏

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  3. ceph源码之一

    转自于:http://blog.csdn.net/changtao381/article/details/8698935 一.概述: 其结构如下:在src 里, 网络通信:  msg  里面 包括了网 ...

  4. SPI通信

    SPI是由Motorola公司提出的一种同步串行外围接口:它在速度要求不高,低功耗,需要保存少量参数的智能化传感系统中得到了广泛应用: SPI是一个全双工的同步串行接口,在数据传输过程中,总线上只能是 ...

  5. java的数据类型,几个java小程序

    1:求圆的面积 还好看了c++,不然直接看这课件还真是看不懂……加油吧 要从键盘读入数据可以用Scanner类的nextlnt()或者nextDouble()方法,首先创建Scanner类的一个实例, ...

  6. No persister for 编译器每行执行两次的解决方法

    是前台的  js  的 datagrid 部件加了 oncheck  事件引起

  7. php 学习笔记 数组3

    15.使用数组 1).并集(union)  array_merge(array1,array2,array3..) 函数把两个或多个数组合并为一个数组,后面覆盖前面 2). 交集(intersecti ...

  8. C#实现时间戳转化

    /// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...

  9. 使用zxing生成二维码 - servlet形式

    因为项目有个功能需要打印二维码,因为我比较喜欢使用html+css+js实现,所以首先想到的是jquery.qrcode.js插件,这个插件可以用canvas和table生成二维码,效果也不错,不过对 ...

  10. linux 远程桌面工具NX

    1.在linux服务器上需要安装3个文件,下载地址为: http://www.nomachine.com/download-package.php?Prod_Id=1977 nxclient-3.4. ...