Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSOUP: Cannot resolve method title()

I am trying to use Jsoup in an android project but it is giving errors. I am using Android Studio. I have added the jsoup jar 1.8.2 to the libs folder and also added the line compile files('libs/jsoup-1.8.2.jar') in the build.gradle file. It is strange as I did not face any such issues with Eclipse. Any suggestions? Thanks in advance !!

protected Void doInBackground(Void... params) {
    try {
               // Connect to website
                Document document = (Document) Jsoup.connect("http://www.example.com/").get();
                // Get the html document title
                websiteTitle = document.title();
                Elements description = document.select("meta[name=description]");
                // Locate the content attribute
                websiteDescription = description.attr("content");
            } catch (IOException e) {
                e.printStackTrace();
    }
    return null;
}

PS: It is also giving the error "Cannot resolve method 'select(java.lang.String)' " for the select method.

like image 830
varunkr Avatar asked Aug 20 '26 12:08

varunkr


1 Answers

You get the error, because a JSoup Document has no method select(String) as you are trying to call.

Instead, you should access the head, which is represented by an Element that allows you to select():

Elements description = document.head().select("meta[name=description]");

On a side note, the explicit cast to Document is not necessary:

Document document = (Document) Jsoup.connect("http://www.example.com/").get();

get() already returns a Document, as you can see in the cookbook or the API docs.

like image 112
TimoStaudinger Avatar answered Aug 23 '26 03:08

TimoStaudinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!