Parse HTML with Jsoup - Java

1

I'm studying the 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 can not handle exception type IOException thrown by   implicit super constructor. Must define an explicit constructor

What does this error mean and how do you solve it? I already 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();
}
    
asked by anonymous 30.10.2015 / 20:19

1 answer

1

The example they provide there is for you to better understand the simplicity of JSoup to parse an html document, but you need to handle the flaws that may occur.

One way to initialize the doc and title attributes is to use the constructor of your Parse class:

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 */
        }
    }
}
    
30.10.2015 / 21:08