微信公众平台java开发附近公交站台功能_第1页
微信公众平台java开发附近公交站台功能_第2页
微信公众平台java开发附近公交站台功能_第3页
微信公众平台java开发附近公交站台功能_第4页
微信公众平台java开发附近公交站台功能_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、微信公众平台java开发附近公交站台功能package.using.tools.aibang;importjava.io.StringReader;importjava.util.ArrayList;importjava.util.List;import.jdom.Document;import.jdom.Element;import.jdom.input.SAXBuilder;import.xml.sax.InputSource;import.usingmon.utils.ExceptionTools;import.usingmon.utils.HttpRequestTools;/*附近公交

2、站台查询*authhma*/publilassAiBangBusStationpublicstaticvoidmain(Stringargs)searchBusLineByNum(南京,1);/*公交线路查询*paramcityName*paramlineNum*/publicstaticList<AiBangBusStationBeansearchBusLineByNum(StringcityName,StringlineNum)StringBuffersbUrl=newStringBuffer();sbUrl.append(aibang/bus/lines);sbUrl.append

3、(app_key=+AiBangIconst.AI_BANG_API_KEY);sbUrl.append(&city=+cityName);/城市sbUrl.append(&q=+lineNum);/距离(单位:米)System.out.println(sbUrl);List<AiBangBusStationBeanlistLine=newArrayList<AiBangBusStationBean();StringpageContext=HttpRequestTools.getHttpClientHtml(sbUrl.toString(),UTF-8);Syste

4、m.out.println(pageContext);if(pageContext!=null&&!.equals(pageContext)SAXBuildersax=newSAXBuilder();tryStringReaderread=newStringReader(pageContext);/创建新的输入源SAX解析器将使用InputSource对象来确定如何读取XML输入InputSourcesource=newInputSource(read);/通过输入源构造一个DocumentDocumentdoc=sax.build(source);/取的根元素Elementr

5、oot=doc.getRootElement();ListlistChild=root.getChild(lines).getChildren();for(inti=0;i<listChild.size();i+)Elementelementstat=(Element)listChild.get(i);AiBangBusStationBeanbeans=newAiBangBusStationBean();beans.setName(elementstat.getChildText(name);Stringlocations=elementstat.getChildText(xy).spl

6、it(,);beans.setLocation_x(locations0);beans.setLocation_y(locations1);beans.setDist(elementstat.getChildText(dist);beans.setLine_names(elementstat.getChildText(line_names);listLine.add(beans);catch(Exceptione)e.printStackTrace();/ExceptionTools.getExceptionDetail(e,获取附近公交站台信息错误!);returnlistLine;/*获取

7、附近公交站台信息*return*/SuppressWarnings(rawtypes)publicstaticList<AiBangBusStationBeangetNearByBusStations(StringcityName,Stringlocation_x,Stringlocation_y)StringBuffersbUrl=newStringBuffer();sbUrl.append(aibang/bus/stats_xy);sbUrl.append(app_key=+AiBangIconst.AI_BANG_API_KEY);sbUrl.append(&city=+c

8、ityName);/城市sbUrl.append(&lng=+location_x);/经度sbUrl.append(&lat=+location_y);/纬度sbUrl.append(&dist=500);/距离(单位:米)System.out.println(sbUrl);List<AiBangBusStationBeanlistNearByBus=newArrayList<AiBangBusStationBean();StringpageContext=HttpRequestTools.getHttpClientHtml(sbUrl.toString(

9、),UTF-8);/System.out.println(sbUrl.toString();if(pageContext!=null&&!.equals(pageContext)SAXBuildersax=newSAXBuilder();tryStringReaderread=newStringReader(pageContext);/创建新的输入源SAX解析器将使用InputSource对象来确定如何读取XML输入InputSourcesource=newInputSource(read);/通过输入源构造一个DocumentDocumentdoc=sax.build(sou

10、rce);/取的根元素Elementroot=doc.getRootElement();ListlistChild=root.getChild(stats).getChildren();for(inti=0;i<listChild.size();i+)Elementelementstat=(Element)listChild.get(i);AiBangBusStationBeanbeans=newAiBangBusStationBean();beans.setName(elementstat.getChildText(name);Stringlocations=elementstat.g

11、etChildText(xy).split(,);beans.setLocation_x(locations0);beans.setLocation_y(locations1);beans.setDist(elementstat.getChildText(dist);beans.setLine_names(elementstat.getChildText(line_names);listNearByBus.add(beans);catch(Exceptione)/e.printStackTrace();ExceptionTools.getExceptionDetail(e,获取附近公交站台信息

12、错误!);listNearByBus=null;returnlistNearByBus;/*根据城市名称,公交站名称,获取公交站台线路信息*paramcityName*paramstationName*return*/SuppressWarnings(rawtypes)publicstaticAiBangBusStationBeangetBusStationDetailByName(StringcityName,StringstationName)StringBuffersbUrl=newStringBuffer();sbUrl.append(aibang/bus/stats);sbUrl.a

13、ppend(app_key=+AiBangIconst.AI_BANG_API_KEY);sbUrl.append(&city=+cityName);/城市sbUrl.append(&q=+stationName);/经度StringpageContext=HttpRequestTools.getHttpClientHtml(sbUrl.toString(),UTF-8);AiBangBusStationBeanbeans=null;SAXBuildersax=newSAXBuilder();tryStringReaderread=newStringReader(pageCon

14、text);/创建新的输入源SAX解析器将使用InputSource对象来确定如何读取XML输入InputSourcesource=newInputSource(read);/通过输入源构造一个DocumentDocumentdoc=sax.build(source);/取的根元素Elementroot=doc.getRootElement();ListlistChild=root.getChild(stats).getChildren();if(listChild!=null&&listChild.size()!=0)Elementelementstat=(Element)l

15、istChild.get(0);beans=newAiBangBusStationBean();beans.setName(elementstat.getChildText(name);Stringlocations=elementstat.getChildText(xy).split(,);beans.setLocation_x(locations0);beans.setLocation_y(locations1);beans.setDist(elementstat.getChildText(dist);beans.setLine_names(elementstat.getChildText

16、(line_names);catch(Exceptione)/e.printStackTrace();ExceptionTools.getExceptionDetail(e,根据城市名称,公交站名称,获取公交站台线路信息错误!);returnbeans;/*获取公交线路详细信息*paramcityName*paramcode*return*/SuppressWarnings(rawtypes)publicstaticAiBangBusStationBeangetgetBusLineDetailInfo(StringcityName,Stringcode)StringBuffersbUrl=ne

17、wStringBuffer();sbUrl.append(aibang/bus/lines);sbUrl.append(app_key=+AiBangIconst.AI_BANG_API_KEY);sbUrl.append(&city=+cityName);/城市sbUrl.append(&q=+code);/经度StringpageContext=HttpRequestTools.getHttpClientHtml(sbUrl.toString(),UTF-8);AiBangBusStationBeanbeans=null;SAXBuildersax=newSAXBuilde

18、r();tryStringReaderread=newStringReader(pageContext);/创建新的输入源SAX解析器将使用InputSource对象来确定如何读取XML输入InputSourcesource=newInputSource(read);/通过输入源构造一个DocumentDocumentdoc=sax.build(source);/取的根元素Elementroot=doc.getRootElement();ListlistChild=root.getChild(lines).getChildren();if(listChild!=null&&li

19、stChild.size()!=0)Elementelementstat=(Element)listChild.get(0);beans=newAiBangBusStationBean();beans.setName(elementstat.getChildText(name);beans.setLineInfo(elementstat.getChildText(info);beans.setLineStats(elementstat.getChildText(stats);beans.setLineStat_xys(elementstat.getChildText(stat_xys);bea

20、ns.setLineXys(elementstat.getChildText(xys);catch(Exceptione)/e.printStackTrace();ExceptionTools.getExceptionDetail(e,获取公交线路详细信息错误!);returnbeans;-package.usingmon.utils;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.HttpURLConnection;importjava.URL;import.apache.http.HttpEnt

21、ity;import.apache.http.HttpResponse;import.apache.http.HttpStatus;import.apache.http.client.HttpClient;import.apache.http.client.methods.HttpGet;import.apache.http.impl.client.DefaultHttpClient;import.apache.http.util.EntityUtils;publilassHttpRequestTools/*请求页面*paramstrUrl*paramstrPostRequest*paramm

22、axLength*paramcode*return*/publicstaticStringgetPageContent(StringstrUrl,StringstrPostRequest,intmaxLength,Stringcode)/读取结果网页StringBufferbuffer=newStringBuffer();HttpURLConnectionconnection=null;tryURLurl=newURL(strUrl);/打开url连接connection=(HttpURLConnection)url.openConnection();/设置url请求方式get或者postco

23、nnection.setRequestMethod(strPostRequest);connection.setConnectTimeout(5000);connection.setReadTimeout(5000);connection.setRequestProperty(User-Agent,Mozilla/4.0(patible;MSIE5.0;WindowsNT;DigExt);/IE代理进行下载/发送BufferedReaderin=newBufferedReader(newInputStreamReader(url.openStream(),code);intch;for(int

24、length=0;(ch=in.read()-1&&(maxLength<=0|length<maxLength);length+)buffer.append(char)ch);/System.out.print(char)ch);in.close();connection.disconnect();returnbuffer.toString().trim();catch(Exceptione)if(connection!=null)connection.disconnect();System.out.println(strUrl+,+e.getMessage();

25、/ExceptionTools.getExceptionDetail(e,地址:+strUrl+请求失败!);returnnull;/*根据URL获得所有的html信息*paramurl*return*/publicstaticStringgetHttpClientHtml(Stringurl,Stringcode)Stringhtml=null;HttpClienthttpClient=newDefaultHttpClient();/创建httpClient对象HttpGethttpget=newHttpGet(url);/以get方式请求该URLtryHttpResponseresponc

26、e=httpClient.execute(httpget);/得到responce对象intresStatu=responce.getStatusLine().getStatusCode();/返回码if(resStatu=HttpStatus.SC_OK)/200正常其他就不对/获得相应实体HttpEntityentity=responce.getEntity();if(entity!=null)html=newString(EntityUtils.toString(entity).getBytes(ISO-8859-1),code);/获得html源代码catch(Exceptione)S

27、ystem.out.println(访问【+url+】出现异常!);e.printStackTrace();finallyhttpClient.getConnectionManager().shutdown();returnhtml;/*默认编码获取HTML代码*paramurl*return*/publicstaticStringgetHttpClientHtml(Stringurl)Stringhtml=null;HttpClienthttpClient=newDefaultHttpClient();/创建httpClient对象HttpGethttpget=newHttpGet(url);/以get方式请求该URLtryHttpResponseres

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论