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;
}
5
  • So your read timeout is too short. Increase it. Commented Mar 13, 2014 at 0: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. Commented Mar 13, 2014 at 0: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. Commented Mar 13, 2014 at 0:32
  • That would be my guess as well. @martynas managed to provide a solution to your suggestion. Thank you. Commented Mar 13, 2014 at 0:35
  • @EJP Comment from 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). jsoup.org/apidocs/org/jsoup/Connection.html" Commented Sep 5, 2014 at 16:15

1 Answer 1

11

Try this:

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

or...

Jsoup.connect(url).timeout(0).get();
Sign up to request clarification or add additional context in comments.

5 Comments

I would guess that the connect(url").get() is a typo after he deleted the actual url. As the code was, it wouldn't compile.
A read timeout isn't a connection-related error. It can only happen if the connection has succeeded.
I mistakenly left a stray " in my code, it's been edited to how it is in my code. It's now been updated
That looks more plausible.
No problem. Please let me know which one did actually work for you so i can update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.