Parse HTML with Jsoup - Java

Asked

Viewed 378 times

1

I am studying Jsoup library and I took the most basic example of the site and tried to build something simple, but I have this error, which does not allow me to execute the code:

Default constructor cannot Handle Exception type Ioexception thrown by implicit super constructor. Must define an Explicit constructor

What does this mistake mean and how to solve it? I’ve researched a lot but the answers are vague.

package estudandoJsoup;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Parse {
    Document doc = Jsoup.connect("http://example.com/").get();
    String title = doc.title();
}

1 answer

1


The example they provide there serves to better understand the simplicity of Jsoup to parse an html document, but you accurate treat failures that may occur.

A way to initialize attributes doc and title is using the constructor of its class Parse:

package estudandoJsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Parse {
    Document doc;
    String title;

    public Parse(){
        try  {
            doc = Jsoup.connect("http://example.com/").get();
            title = doc.title();
        } catch(IOException err){
            /* Tratamento */
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.