2016-11-23 5 views
-1

私はYelpからビジネス情報を抽出して出力するプログラムを持っています。最終的にjava.net.SocketTimeoutExceptionに達するまで、すべてがコンパイルされ、しばらく実行されます。私はこの問題に関するいくつかの調査を行いました。明らかに、それはネットワークの問題です。解決策はランタイムタイムアウトを追加することです。ここでは、私はそれがどのように行われたのか、それを私のコードにどのように実装するのかを知りません。ここに私が持っているものがあります: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

あなたは常にこのdocのようにタイムアウトを使用することができます:あなたは、実行時のタイムアウトが必要という結論にあなたをリードしてきました何

document = Jsoup.connect(url).timeout(3000).get(); 
関連する問題