dimanche 3 juillet 2016

How do I continuously retry a JSoup request?

I am trying to use JSoup to get some data and write it to a file, however I have a requests that I need. Sometimes I get a 404 error (even with agent header).

How can I keep retrying the request until it works and only write to the text file when I know it successfully received a response?

The code I have is below.

        for (String link : listOfLinks(doc)) {
                try {
                    Document doc= Jsoup.connect(link).userAgent("Mozilla").get();

                    Elements ps = doc.select("p");

                    writeTextToFile(ps.text());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

I have tried putting it wrapped in a loop, perhaps I am not doing it correctly. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire