java.net.SocketTimeoutException: Read timed out error when trying to read from table

AndyReifman picture AndyReifman · Mar 13, 2014 · Viewed 10.8k times · Source

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;
}

Answer

martynas picture martynas · Mar 13, 2014

Try this:

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

or...

Jsoup.connect(url).timeout(0).get();