JSOUP: Cannot resolve method name ()

I am trying to use Jsoup in android project but it gives errors. I am using Android Studio. I added jsoup jar 1.8.2 to libs folder and also added line compilation files ('libs / jsoup-1.8.2.jar') in build.gradle file. Strange as I have not encountered 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 also gives error "Unable to resolve method" select (java.lang.String) "for select method.

+3


source to share


1 answer


You are getting the error because JSoup Document

does not have the method select(String)

you are trying to call.

Instead, you have to access the head , which is presented Element

, which allows you to select()

:

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

      




In a side note, no explicit cast to Document

is required:

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

      

get()

already returns Document

, as you can see in the cookbook or API docs .

+2


source







All Articles