【问题标题】:How do I add a runtime timeout to prevent java.net.SocketTimeoutException?如何添加运行时超时以防止 java.net.SocketTimeoutException?
【发布时间】:2017-04-08 00:23:32
【问题描述】:

所以我有一个程序可以从 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;

        }
    }
}

我需要怎么做才能添加运行时超时?

【问题讨论】:

  • 是什么让您得出需要运行时超时的结论?

标签: java exception timeout runtime jsoup


【解决方案1】:

你总是可以像在这个文档中那样使用超时:

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

像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多