問題描述
如何添加運行時超時以防止 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 Woodruff、JFPicard)