??????????httpclient????????????????????2???????????????????httpclient???????get????????????fastJson???????????????????????catchTrainInfo()????????
????import java.io.UnsupportedEncodingException;
????import java.net.URLEncoder;
????import java.util.HashMap;
????import java.util.HashSet;
????import java.util.Iterator;
????import java.util.Map;
????import java.util.Map.Entry;
????import javax.annotation.Resource;
????import org.slf4j.Logger;
????import org.slf4j.LoggerFactory;
????import java.util.Set;
????public class CatchTrainInfo {
????/**
????* ????????
????* @param attachmentId
????* @return
????*/
????@SuppressWarnings("unchecked")
????public  String catchTrainInfo() {
????//??????
????HashSet<String> station = new HashSet<String>();
????//?????????
????HashSet<String> stationTmp = new HashSet<String>();
????//???????
????Map<String??String> train = new HashMap<String??String>();
????String stationHtml = HttpClientUtil.get("http://www.59178.com/zhan/");
????String stationName = "";
????//?????????
????String stations[] = stationHtml.split("htm'>");
????for (int i = 0; i < stations.length; i++) {
????if (i == 0 ) {
????continue;
????}
????stationName = getStation(stations[i]);
????if("".equals(stationName)) {
????continue;
????}
????station.add(stationName);
????}
????//????????????????????????
????getTrainsByStation(station??train);
????//???????????λ?????????飬?????????
????getTrainDetailsByTrains(stationTmp??train);
????//???????3???????(???????stationTmp.size()==0???????????????)
????int loop = 3;
????while (loop > 0) {
????//stationTmp ?? station????stationTmp?????????????????station??stationTmp????????????
????HashSet<String> stationTmp1 = (HashSet<String>) stationTmp.clone();
????for (String stationTmpElement : stationTmp) {
????for (String stationElement : station) {
????if(stationTmpElement.equals(stationElement)) {
????stationTmp.remove(stationTmpElement);
????}
????}
????}
????station = stationTmp1;
????//????????????????????????
????getTrainsByStation(stationTmp??train);
????//???????????λ?????????飬??д???????
????getTrainDetailsByTrains(stationTmp??train);
????loop--;
????}
????return "success!";
????}
????/**
????* ??????λ?????????飬??д???????
????* @param stationTmp
????* @param train
????*/
????private  void getTrainDetailsByTrains(HashSet<String> stationTmp?? Map<String?? String> train) {
????Iterator<Entry<String?? String>> iterator = train.entrySet().iterator();
????while (iterator.hasNext()) {
????Entry<String?? String> entry = iterator.next();
????String key =  entry.getKey();
????String value =  entry.getValue();
????if("unused".equals(value)) {
????getAndSaveTrainsDetails(key??stationTmp);
????entry.setValue("used");
????}
????try {
????//????????????????
????Thread.sleep(100);
????} catch (InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????/**
????* ???????????????????
????* @param station
????* @param train
????*/
????private  void getTrainsByStation(HashSet<String> station?? Map<String?? String> train) {
????Iterator<String> iterator = station.iterator();
????while (iterator.hasNext()) {
????String key = iterator.next();
????getTrainsInfo(train??key);
????try {
????//????????????????
????Thread.sleep(100);
????} catch (InterruptedException e) {
????e.printStackTrace();
????}
????}
????}
????/**
????* ???????????????????
????* @param train
????* @param stationName
????* @return
????*/
????private  void getTrainsInfo(Map<String?? String> train?? String stationName) {
????try {
????stationName = URLEncoder.encode(stationName??"utf-8");
????} catch (UnsupportedEncodingException e1) {
????log.error("?????????stationName:" + stationName);
????}
????String url = "http://train.qunar.com/qunar/stationInfo.jsp?q=" + stationName + "&format=json";
????//????????????????  ticketInfo
????String ticketInfo = HttpClientUtil.get(url);
????try{
????@SuppressWarnings("unchecked")
????java.util.Map<String?? Object> ticketInfos = (Map<String?? Object>) JSONObject.parseObject(ticketInfo?? java.util.Map.class).get("ticketInfo");
????Set<Entry<String?? Object>> entries = ticketInfos.entrySet( );
????if (entries != null) {
????Iterator<Entry<String?? Object>> iterator = entries.iterator( );
????while (iterator.hasNext( )) {
????Entry<String?? Object> entry = iterator.next();
????String key = (String) entry.getKey( );
????String trains[] = key.split("/");
????for(int i = 0; i< trains.length; i++) {
????train.put(trains[i]?? "unused");
????}
????}
????}
????} catch (Exception e) {
????log.info("????????????????????????"+e.getMessage());
????}
????}
????/**
????* ?????????????
????* @param str
????* @return
????*/
????private  String getStation(String str) {
????if (str == null || str.length() <= 0) {
????return "";
????}
????int pos = -1;
????pos = str.indexOf("</a>"?? pos + 1);
????if (pos == -1) {
????return "";
????}
????return str.substring(0?? pos);
????}
????/**
????* ??????λ?????????飬?????????
????*
????* @param key
????* @param stationTmp
????*/
????public void getAndSaveTrainsDetails(String key?? HashSet<String> stationTmp) {
????String url = "http://train.qunar.com/qunar/checiInfo.jsp?q=" + key + "&date=20170107&format=json";
????String trainScheduleBody = HttpClientUtil.getUtf8(url);
????try {
????List<Object> ticketInfos = (List<Object>) JSONObject.parseObject(trainScheduleBody?? Map.class)
????.get("trainScheduleBody");
????for (int i = 0; i < ticketInfos.size(); i++) {
????List<String> list = (List<String>) JSONObject.parseObject(ticketInfos.get(i).toString()?? Map.class)
????.get("content");
????if (list == null || list.size() <= 0) {
????continue;
????}
????stationTmp.add(list.get(1));
????// ??????飬???????
????//TODO 
????}
????} catch (Exception e) {
????log.info("??????λ?????????鱨???" + e.getMessage());
????}
????}
????}