如何添加运行时超时以防止java.net.SocketTimeoutException?

时间:2016-11-23 20:43:52

标签: java exception timeout runtime jsoup

所以我有一个程序从Yelp中提取业务信息并输出它。一切都编译,并运行一段时间,直到它最终命中java.net.SocketTimeoutException。我对这个问题进行了一些研究,显然它是网络的问题,解决方案是添加运行时超时。事情就是这样,我不知道它是如何完成的,也不知道如何将它实现到我的代码中。这就是我得到的:

import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.Scanner;

public class YelpScraper
{
    public static void main(String[] args) throws IOException, Exception, RuntimeException
    {        
        //Variables
        String description;
        String location;
        int pages;
        int parseCount = 0;
        Document document;

        Scanner keyboard = new Scanner(System.in);

        //Perform a Search
        System.out.print("Enter a description: ");
        description = keyboard.nextLine();

        System.out.print("Enter a state: ");
        location = keyboard.nextLine();

        System.out.print("How many pages should we scan? ");
        pages = keyboard.nextInt();

        String descString = "find_desc=" + description.replace(' ', '+') + "&";
        String locString = "find_loc=" + location.replace(' ', '+') + "&";
        int number = 0;

        String url = "https://www.yelp.com/search?" + descString + locString + "start=" + number;
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> address = new ArrayList<String>();
        ArrayList<String> phone = new ArrayList<String>();

        //Fetch Data From Yelp
        for (int i = 0 ; i <= pages ; i++)
        {

            document = Jsoup.connect(url).get();

            Elements nameElements = document.select(".indexed-biz-name span");
            Elements addressElements = document.select(".secondary-attributes address");
            Elements phoneElements = document.select(".biz-phone");

            for (Element element : nameElements)
            {
                names.add(element.text());
            }

            for (Element element : addressElements)
            {
                address.add(element.text());
            }

            for (Element element : phoneElements)
            {
                phone.add(element.text());
            }

            for (int index = 0 ; index < 10 ; index++)
            {
                System.out.println("\nLead " + parseCount);
                System.out.println("Company Name: " + names.get(parseCount));
                System.out.println("Address: " + address.get(parseCount));
                System.out.println("Phone Number: " + phone.get(parseCount));

                parseCount = parseCount + 1;
            }

            number = number + 10;

        }
    }
}

添加运行时超时需要做什么?

1 个答案:

答案 0 :(得分:1)

您可以随时使用此文档中的超时:

https://jsoup.org/cookbook/input/load-document-from-url

像这样:

document = Jsoup.connect(url).timeout(3000).get();