JSOUP:无法解析方法标题()
JSOUP: Cannot resolve method title()
我正在尝试在 android 项目中使用 Jsoup,但出现错误。我正在使用 Android Studio。我已将 jsoup jar 1.8.2 添加到 libs 文件夹,并在 build.gradle 文件中添加了行 compile files('libs/jsoup-1.8.2.jar') 。这很奇怪,因为我没有遇到 Eclipse 的任何此类问题。有什么建议么?提前致谢!!
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:select 方法也出现错误 "Cannot resolve method 'select(java.lang.String)' "。
您收到错误消息,因为 JSoup Document
没有您尝试调用的方法 select(String)
。
相反,您应该 access the head, which is represented by an Element
that allows you to select()
:
Elements description = document.head().select("meta[name=description]");
附带说明,没有必要显式转换为 Document
:
Document document = (Document) Jsoup.connect("http://www.example.com/").get();
get()
已经 returns 一个 Document
,如您所见 in the cookbook or the API docs.
我正在尝试在 android 项目中使用 Jsoup,但出现错误。我正在使用 Android Studio。我已将 jsoup jar 1.8.2 添加到 libs 文件夹,并在 build.gradle 文件中添加了行 compile files('libs/jsoup-1.8.2.jar') 。这很奇怪,因为我没有遇到 Eclipse 的任何此类问题。有什么建议么?提前致谢!!
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:select 方法也出现错误 "Cannot resolve method 'select(java.lang.String)' "。
您收到错误消息,因为 JSoup Document
没有您尝试调用的方法 select(String)
。
相反,您应该 access the head, which is represented by an Element
that allows you to select()
:
Elements description = document.head().select("meta[name=description]");
附带说明,没有必要显式转换为 Document
:
Document document = (Document) Jsoup.connect("http://www.example.com/").get();
get()
已经 returns 一个 Document
,如您所见 in the cookbook or the API docs.