package com.using.tools.aibang;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
import com.using.common.utils.ExceptionTools;
import com.using.common.utils.HttpRequestTools;
/**
* 附近公交站臺查詢
* @author ghma
*
*/
public class AiBangBusStation {
public static void main(String[] args) {
searchBusLineByNum("南京", "1");
}
/**
* 公交線路查詢
* @param cityName
* @param lineNum
*/
public static List
StringBuffer sbUrl = new StringBuffer();
sbUrl.append("http://openapi.aibang.com/bus/lines");
sbUrl.append("?app_key=" + AiBangIconst.AI_BANG_API_KEY);
sbUrl.append("&city=" + cityName); // 城市
sbUrl.append("&q=" + lineNum); // 距離(單位:米)
System.out.println(sbUrl);
List
String pageContext = HttpRequestTools.getHttpClientHtml(sbUrl.toString(),"UTF-8");
System.out.println(pageContext);
if(pageContext != null && !"".equals(pageContext)) {
SAXBuilder sax = new SAXBuilder();
try {
StringReader read = new StringReader(pageContext);
// 建立新的輸入源SAX 解析器將使用 InputSource 物件來確定如何讀取 XML 輸入
InputSource source = new InputSource(read);
// 通過輸入源構造一個Document
Document doc = sax.build(source);
// 取的根元素
Element root = doc.getRootElement();
List listChild = root.getChild("lines").getChildren();
for (int i = 0; i < listChild.size(); i++) {
Element elementstat = (Element) listChild.get(i);
AiBangBusStationBean beans = new AiBangBusStationBean();
beans.setName(elementstat.getChildText("name"));
String[] locations = elementstat.getChildText("xy").split(",");
beans.setLocation_x(locations[0]);
beans.setLocation_y(locations[1]);
beans.setDist(elementstat.getChildText("dist"));
beans.setLine_names(elementstat.getChildText("line_names"));
listLine.add(beans);
}
} catch (Exception e) {
e.printStackTrace();
//ExceptionTools.getExceptionDetail(e, "獲取附近公交站臺資訊錯誤!");
}
}
return listLine;
}
/**
* 獲取附近公交站臺資訊
* @return
*/
@SuppressWarnings("rawtypes")
public static List
StringBuffer sbUrl = new StringBuffer();
sbUrl.append("http://openapi.aibang.com/bus/stats_xy");
sbUrl.append("?app_key=" + AiBangIconst.AI_BANG_API_KEY);
sbUrl.append("&city=" + cityName); // 城市
sbUrl.append("&lng="+ location_x); // 經度
sbUrl.append("&lat=" + location_y); // 緯度
sbUrl.append("&dist=500"); // 距離(單位:米)
System.out.println(sbUrl);
List
String pageContext = HttpRequestTools.getHttpClientHtml(sbUrl.toString(),"UTF-8");
//System.out.println(sbUrl.toString());
if(pageContext != null && !"".equals(pageContext)) {
SAXBuilder sax = new SAXBuilder();
try {
StringReader read = new StringReader(pageContext);
// 建立新的輸入源SAX 解析器將使用 InputSource 物件來確定如何讀取 XML 輸入
InputSource source = new InputSource(read);
// 通過輸入源構造一個Document
Document doc = sax.build(source);
// 取的根元素
Element root = doc.getRootElement();
List listChild = root.getChild("stats").getChildren();
for (int i = 0; i < listChild.size(); i++) {
Element elementstat = (Element) listChild.get(i);
AiBangBusStationBean beans = new AiBangBusStationBean();
beans.setName(elementstat.getChildText("name"));
String[] locations = elementstat.getChildText("xy").split(",");
beans.setLocation_x(locations[0]);
beans.setLocation_y(locations[1]);
beans.setDist(elementstat.getChildText("dist"));
beans.setLine_names(elementstat.getChildText("line_names"));
listNearByBus.add(beans);
}
} catch (Exception e) {
//e.printStackTrace();
ExceptionTools.getExceptionDetail(e, "獲取附近公交站臺資訊錯誤!");
listNearByBus = null;
}
}
return listNearByBus;
}
/**
* 根據城市名稱,公交站名稱,獲取公交站臺線路資訊
* @param cityName
* @param stationName
* @return
*/
@SuppressWarnings("rawtypes")
public static AiBangBusStationBean getBusStationDetailByName(String cityName, String stationName) {
StringBuffer sbUrl = new StringBuffer();
sbUrl.append("http://openapi.aibang.com/bus/stats");
sbUrl.append("?app_key=" + AiBangIconst.AI_BANG_API_KEY);
sbUrl.append("&city=" + cityName); // 城市
sbUrl.append("&q="+ stationName); // 經度
String pageContext = HttpRequestTools.getHttpClientHtml(sbUrl.toString(),"UTF-8");
AiBangBusStationBean beans = null;
SAXBuilder sax = new SAXBuilder();
try {
StringReader read = new StringReader(pageContext);
// 建立新的輸入源SAX 解析器將使用 InputSource 物件來確定如何讀取 XML 輸入
InputSource source = new InputSource(read);
// 通過輸入源構造一個Document
Document doc = sax.build(source);
// 取的根元素
Element root = doc.getRootElement();
List listChild = root.getChild("stats").getChildren();
if(listChild != null && listChild.size() != 0) {
Element elementstat = (Element) listChild.get(0);
beans = new AiBangBusStationBean();
beans.setName(elementstat.getChildText("name"));
String[] locations = elementstat.getChildText("xy").split(",");
beans.setLocation_x(locations[0]);
beans.setLocation_y(locations[1]);
beans.setDist(elementstat.getChildText("dist"));
beans.setLine_names(elementstat.getChildText("line_names"));
}
} catch (Exception e) {
//e.printStackTrace();
ExceptionTools.getExceptionDetail(e, "根據城市名稱,公交站名稱,獲取公交站臺線路資訊錯誤!");
}
return beans;
}
/**
* 獲取公交線路詳細資訊
* @param cityName
* @param code
* @return
*/
@SuppressWarnings("rawtypes")
public static AiBangBusStationBean getgetBusLineDetailInfo(String cityName, String code) {
StringBuffer sbUrl = new StringBuffer();
sbUrl.append("http://openapi.aibang.com/bus/lines");
sbUrl.append("?app_key=" + AiBangIconst.AI_BANG_API_KEY);
sbUrl.append("&city=" + cityName); // 城市
sbUrl.append("&q="+ code); // 經度
String pageContext = HttpRequestTools.getHttpClientHtml(sbUrl.toString(),"UTF-8");
AiBangBusStationBean beans = null;
SAXBuilder sax = new SAXBuilder();
try {
StringReader read = new StringReader(pageContext);
// 建立新的輸入源SAX 解析器將使用 InputSource 物件來確定如何讀取 XML 輸入
InputSource source = new InputSource(read);
// 通過輸入源構造一個Document
Document doc = sax.build(source);
// 取的根元素
Element root = doc.getRootElement();
List listChild = root.getChild("lines").getChildren();
if(listChild != null && listChild.size() != 0) {
Element elementstat = (Element) listChild.get(0);
beans = new AiBangBusStationBean();
beans.setName(elementstat.getChildText("name"));
beans.setLineInfo(elementstat.getChildText("info"));
beans.setLineStats(elementstat.getChildText("stats"));
beans.setLineStat_xys(elementstat.getChildText("stat_xys"));
beans.setLineXys(elementstat.getChildText("xys"));
}
} catch (Exception e) {
//e.printStackTrace();
ExceptionTools.getExceptionDetail(e, "獲取公交線路詳細資訊錯誤!");
}
return beans;
}
}
---------------------------------------
package com.using.common.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class HttpRequestTools {
/**
* 請求頁面
* @param strUrl
* @param strPostRequest
* @param maxLength
* @param code
* @return
*/
public static String getPageContent(String strUrl, String strPostRequest, int maxLength, String code) {
// 讀取結果網頁
StringBuffer buffer = new StringBuffer();
HttpURLConnection connection = null;
try {
URL url = new URL(strUrl);
// 開啟url連線
connection = (HttpURLConnection) url.openConnection();
// 設定url請求方式 ‘get’ 或者 ‘post’
connection.setRequestMethod(strPostRequest);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// IE代理進行下載
// 傳送
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), code));
int ch;
for (int length = 0; (ch = in.read()) > -1 && (maxLength <= 0 length < maxLength); length++) {
buffer.append((char) ch);
//System.out.print((char) ch);
}
in.close();
connection.disconnect();
return buffer.toString().trim();
} catch (Exception e) {
if(connection != null) {
connection.disconnect();
}
System.out.println(strUrl + "," + e.getMessage());
//ExceptionTools.getExceptionDetail(e, "地址:" + strUrl + "請求失敗!");
return null;
}
}
/**
* 根據URL獲得所有的html資訊
*
* @param url
* @return
*/
public static String getHttpClientHtml(String url,String code) {
String html = null;
HttpClient httpClient = new DefaultHttpClient();// 建立httpClient物件
HttpGet httpget = new HttpGet(url);// 以get方式請求該URL
try {
HttpResponse responce = httpClient.execute(httpget);// 得到responce物件
int resStatu = responce.getStatusLine().getStatusCode();// 返回碼
if (resStatu == HttpStatus.SC_OK) {// 200正常 其他就不對
// 獲得相應實體
HttpEntity entity = responce.getEntity();
if (entity != null) {
html = new String(EntityUtils.toString(entity).getBytes("ISO-8859-1"),code);// 獲得html原始碼
}
}
} catch (Exception e) {
System.out.println("訪問【" + url + "】出現異常!");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return html;
}
/**
* 預設編碼獲取HTML程式碼
* @param url
* @return
*/
public static String getHttpClientHtml(String url) {
String html = null;
HttpClient httpClient = new DefaultHttpClient();// 建立httpClient物件
HttpGet httpget = new HttpGet(url);// 以get方式請求該URL
try {
HttpResponse responce = httpClient.execute(httpget);// 得到responce物件
int resStatu = responce.getStatusLine().getStatusCode();// 返回碼
if (resStatu == HttpStatus.SC_OK) {// 200正常 其他就不對
// 獲得相應實體
HttpEntity entity = responce.getEntity();
if (entity != null) {
html = EntityUtils.toString(entity);// 獲得html原始碼
}
}
} catch (Exception e) {
System.out.println("訪問【" + url + "】出現異常!");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return html;
}
}
package com.using.common.utils;
import com.using.common.email.MailTools;
public class ExceptionTools {
/**
* 獲取異常發生郵件進行通知
* @param e
*/
public static void getExceptionDetail(Exception e, String details) {
StringBuffer sb = new StringBuffer(details + "
" + e.getMessage() + "
");
StackTraceElement[] elements = e.getStackTrace();
for (int i = 0; i < elements.length; i++) {
sb.append("at " + elements[i].getClassName() + " " + elements[i].getMethodName() + "(" + elements[i].getFileName() +":"+elements[i].getLineNumber() + ")" + "
");
}
// 發生郵件
MailTools.sendMail(sb.toString());
}
}
原作者: 小海