6

I'm working on coding a program that will go and retrieve the price of a person from a table on a website. The code gets a last name and searches the table for that name before returning the price (a different column) whenever I run it I get a java.net.SocketTimeoutException: Read timed out

This is the code I'm using to query the website

public String price(String lastName) throws IOException
{
    Document doc = Jsoup.connect(url).get();

    Elements rows = doc.getElementsByTag("tr");;

    for(Element row : rows)
    {
        Elements columns = row.getElementsByTag("td");
        String lastName = columns.get(0).text();
        String price = columns.get(2).text();
        if(lastName.equalsIgnoreCase(name))
        {
            return price;
        }
    }
    return null;
}
AndyReifman
  • 1,459
  • 4
  • 18
  • 34
  • So your read timeout is too short. Increase it. – user207421 Mar 13 '14 at 00:23
  • @EJP Any suggestions on how to do that? A quick Google search suggested `Connection timeout(int millis)` but I'm not sure where I would place that in my code. – AndyReifman Mar 13 '14 at 00:30
  • No, that sets the connection timeout. I don't know anything about JSoup but the default socket read timeout in Java is infinite so somebody somewhere must have changed it in JSoup. – user207421 Mar 13 '14 at 00:32
  • That would be my guess as well. @martynas managed to provide a solution to your suggestion. Thank you. – AndyReifman Mar 13 '14 at 00:35
  • @EJP Comment from [iManage user](http://stackoverflow.com/users/3643213/imanage-user): "apparently in Jsoup "Connection.timeout(int millis)" actually sets the timeout for both connection and read, according to the documentation: Connection timeout(int millis) Set the request timeouts (connect and read). http://jsoup.org/apidocs/org/jsoup/Connection.html" – David Eisenstat Sep 05 '14 at 16:15

1 Answers1

11

Try this:

Jsoup.connect(url).timeout(60*1000).get(); 

or...

Jsoup.connect(url).timeout(0).get();
martynas
  • 12,120
  • 3
  • 55
  • 60