如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)


問題描述

如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)

所以我有一個程序可以從 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:

You can always use timeout like in this doc:

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

like this:

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

(by Brandon WoodruffJFPicard)

參考文件

  1. How do I add a runtime timeout to prevent java.net.SocketTimeoutException? (CC BY‑SA 2.5/3.0/4.0)

#runtime #java #jsoup #Exception #timeout






相關問題

通過java運行的執行程序似乎超時? (Executed programs run through java seem to time out?)

Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)

Hentikan proses anak ketika proses induk berhenti (Stop child process when parent process stops)

Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console-script)

選擇不包含主要類型 - 錯誤 (Selection does not contain main type - error)

在活動之間使用意圖錯誤 (using intents between activities error)

在運行時使用 Spring 在表中創建新字段 (Create new field into a table with Spring at runtime)

如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)

我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)

如何計算此代碼的時間複雜度? (How can I compute the time complexity of this code?)

運行 .exe 文件生成 bt jar2exe 軟件時出現 java runtime environment not found 錯誤 (Getting java runtime enviroment not found error while running .exe file produced bt jar2exe software)

在運行時在 VBA 中顯示所有變量及其值 (Show all variables and their values in VBA during runtime)







留言討論