苏州公交即时信息获取(Android)

package com.ximu.bus;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BusInfo {
public ArrayList<HashMap<String, String>> getWayInfo(String url){
String html = get("http://m.sz-map.com/"+url);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Pattern pa = Pattern.compile("<td id=\"(.*?)\" class=\"(.*?)\" >.*?<a class=\".*?\".*?href=\"(.*?)\">(.*?)</a>.*?<span.*?>(.*?)</span>.*?</td>");
Matcher ma = pa.matcher(html);
while(ma.find()){
HashMap<String, String> info = new HashMap<String, String>();
info.put("id", ma.group(1).trim());
info.put("class", ma.group(2).trim());
info.put("href", ma.group(3).trim());
info.put("name", ma.group(4).trim());
info.put("time", ma.group(5).trim());
list.add(info);
}
return list;
}

public ArrayList<HashMap<String, String>> getWay(String key){
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String> param = new HashMap<String, String>();
param.put("kw", key);
param.put("paging","y");
param.put("st", "1");
String html = post("http://m.sz-map.com/search",param);
//正则得到线路信息
Pattern pa = Pattern.compile("<table>.*?<tr>.*?<th rowspan=\"2\">.*?<a href=\"(.*?)\">(.*?)<span style.*?<span>(.*?)</span></a>.*?<div class=\"line_start\">(.*?)</div>.*?<div class=\"line_end\" >(.*?)</div>");
Matcher ma = pa.matcher(html);
while(ma.find()){
HashMap<String, String> info = new HashMap<String, String>();
info.put("way", ma.group(2).trim());
info.put("href", ma.group(1).trim());
info.put("start", ma.group(4).trim());
info.put("end", ma.group(5).trim());
info.put("total", ma.group(3).trim());
// info.put("start-time", ma.group(5).trim());
// info.put("end-time", ma.group(5).trim());
list.add(info);
}
return list;
}

/**
* get
*
@param httpurl
*
@return
*/
private String get(String httpurl){
String line = "";
String html = "";
try {
URL url = new URL(httpurl);
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
InputStream in = httpconn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while((line=br.readLine())!=null){
html += line;
}
} catch (Exception e) {
return "";
}
return html;
}

/**
* post
*
@return
*/
private String post(String url,HashMap<String, String> param){
String line = "";
String html = "";
String paramstr = "";
try{
URL httpURL = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) httpURL.openConnection();
Iterator iter = param.keySet().iterator();
while (iter.hasNext()) {
String key = (String)iter.next();
String value = (String)param.get(key);
paramstr += key+"="+value+"&";
}
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.getOutputStream().write(paramstr.getBytes());
InputStream in = httpConn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while((line=br.readLine())!=null){
html += line;
}
}catch(Exception e){
return "";
}
return html;
}
}

从m.sz-map.com 分离出苏州公交的即时信息,可以开发和此相关的应用。待完善

原文地址:https://www.cnblogs.com/ximu/p/2363139.html