edu.se.ustc.ClientWithResponseHandler.java Source code

Java tutorial

Introduction

Here is the source code for edu.se.ustc.ClientWithResponseHandler.java

Source

/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package edu.se.ustc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import edu.se.ustc.item.BusRouteInfo;
import edu.se.ustc.util.dbUtil;

import java.sql.*;

/**
 * This example demonstrates the use of the {@link ResponseHandler} to simplify
 * the process of processing the HTTP response and releasing associated
 * resources.
 */
public class ClientWithResponseHandler implements Runnable {
    /**
     * the main handler function of Crawls
     * @throws Exception
     */
    private String fileName; //
    private String URL; //URL
    private static boolean startFlag; //

    public ClientWithResponseHandler(String fileName, String URL) {
        this.fileName = fileName;
        this.URL = URL;
    }

    public List<BusRouteInfo> run(String URL) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        List<BusRouteInfo> bsrList = new ArrayList<BusRouteInfo>();
        try {
            HttpGet httpget = new HttpGet(URL);
            //debug
            //System.out.println("Executing request " + httpget.getRequestLine());

            // Create a custom response handler
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

                public String handleResponse(final HttpResponse response)
                        throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }

            };
            String responseBody = httpclient.execute(httpget, responseHandler);
            //System.out.println("----------------------------------------");

            //System.out.println(responseBody);

            String str = responseBody.substring(responseBody.indexOf("table"), responseBody.lastIndexOf("table"));
            //debug
            //System.out.println(str);

            BusRouteInfo binfo = new BusRouteInfo();
            bsrList = binfo.getBusRouteInfoList(str);

            //debug, print all infomation
            //binfo.printBusRouteInfo(bsrList);

            //select the db
            //dbUtil db = new dbUtil();
            //result=db.executeQuery("SELECT * FROM station_stats");
            //         if(result!=null)
            //             System.out.println(result.getString(1));
        } finally {
            httpclient.close();
        }

        return bsrList;
    }

    //   public static void main(String[] agrs){
    //      
    //      //ClientWithResponseHandler cwrh  = new ClientWithResponseHandler();
    //      //
    //      //String address = "http://www.szjt.gov.cn/BusQuery/APTSLine.aspx?cid=175ecd8d-c39d-4116-83ff-109b946d7cb4&LineGuid=9acf55b9-8406-40ef-8056-6de249174ee0&LineInfo=19(%E6%96%B0%E7%81%AB%E8%BD%A6%E7%AB%99%E5%8C%97%E4%B8%B4%E6%97%B6%E5%B9%BF%E5%9C%BA)";
    //      //2
    //      String address ="http://www.szjt.gov.cn/BusQuery/APTSLine.aspx?cid=175ecd8d-c39d-4116-83ff-109b946d7cb4&LineGuid=e0e5561a-32ea-432d-ac98-38eed8c4e448&LineInfo=%E5%BF%AB%E7%BA%BF2%E5%8F%B7(%E7%8B%AC%E5%A2%85%E6%B9%96%E9%AB%98%E6%95%99%E5%8C%BA%E9%A6%96%E6%9C%AB%E7%AB%99=%3E%E7%81%AB%E8%BD%A6%E7%AB%99)";
    //      try {
    //         cwrh.run(address);
    //      } catch (Exception e) {
    //         // TODO Auto-generated catch block
    //         e.printStackTrace();
    //      }
    //      
    //   }

    //
    public static void setStartFlag(boolean flag) {
        startFlag = flag;
    }

    //
    public static boolean getStartFlag() {
        return startFlag;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        if (getStartFlag()) {
            System.out.println(fileName + " thread is start.");
        } else {
            System.out.println(fileName + " thread is closed.");
        }
        ClientWithResponseHandler cwrh = new ClientWithResponseHandler(fileName, URL);
        //list, 
        List<BusRouteInfo> oldList = new ArrayList<BusRouteInfo>();
        List<BusRouteInfo> bsiList = new ArrayList<BusRouteInfo>();
        try {
            oldList = cwrh.run(URL);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while (getStartFlag()) {
            try {
                bsiList = cwrh.run(URL);
                Thread.currentThread().sleep(5000);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            BusRouteInfo bri = new BusRouteInfo();
            if (oldList != null && bsiList != null) {
                try {
                    if (bri.saveToTxt(fileName, oldList, bsiList)) {
                        oldList = bsiList;
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}