Free api interface code of public transit lines and api interfaces of public transit lines

Source: Internet
Author: User

Free api interface code of public transit lines and api interfaces of public transit lines

Description: This interface is used to perform fuzzy search for urban public transit line information based on the city name + line name.

Open source api interface: http://openapi.aibang.com/bus/lines? App_key = f41c8afccc586de03a99c86097e98ccb & city = "+ cityName +" & q = "+ line

CityName = URLEncoder. encode (cityName, "UTF-8 ")

Line = URLEncoder. encode (line, "UTF-8 ")

 

The code is implemented as follows:

Package org. wx. xhelper. model;/*** Public Transport Information Entity class * @ author wangxw * @ version 1.0 * @ date Jul 8, 2014 8:15:49 AM */public class Bus {// number of results private int resultNum; // line name private String name; // line information private String info; // site private String stats along the way; // site longitude and latitude private String statXys; // line coordinate private String xys; // description 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 ;}}


Code for implementing the interface service class:

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;/*** interface class of Bus query service * @ author wangxw * @ version 1.0 * @ da Te Jul 2, 2014 1:55:14 */public class BusQueryService {/*** generate bus query information * @ return bus info * @ throws UnsupportedEncodingException */public static String getBusInfoDetail (String cityName, string line) throws UnsupportedEncodingException {// obtain the line information List <Bus> buslist = getBusInfo (cityName, line); // store the text information 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");} else {news. append (buslist. get (I ). getDescription () ;}}} if (news. length () = 0) {news. append ("no "). append (line ). append ("this line, please enter an accurate line! ");} Return news. toString ();}/*** obtain line information * @ 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 ;}}



Optimization of query line results:

Because the line query uses fuzzy query, there are multiple query results.

For example, if you enter Suzhou + 2, the query result will be: there are multiple types of information, such as road 2, road 2, road 2, express line 2, line 2 for customs declaration and inspection, and line 2 for rail transit. If you want to perform exact search, the following improvements are made:

Filter out line information not starting with 2

// Store the text information 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");} else {news. append (buslist. get (I ). getDescription ());}}}}



 

 


Can Baidu map APIs be called for free? Need a piece of js Code to join the website?

Baidu maps are now available for free.
For details, see pai.baidu.com/map/

How to find the api code of your website

Cannot be found.
The website's api interfaces are self-built. What APIs do you need to provide to others.
Of course, some CMS programs have built-in API interfaces related to members and articles. If you are referring to this API, you can simply find it in the file directory. Generally, CMS members, such as PHPCMS, and other modules have APIs in the directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.