android html下载和解析错误

时间:2011-01-10 23:40:53

标签: android html jsoup

我正在尝试使用页面的ul下载html文件。我正在使用Jsoup。 这是我的代码:

TextView ptext = (TextView) findViewById(R.id.pagetext);
    Document doc = null;
    try {
         doc = (Document) Jsoup.connect(mNewLinkUrl).get();
    } catch (IOException e) {
        Log.d(TAG, e.toString());
        e.printStackTrace();
    }
    NodeList nl = doc.getElementsByTagName("meta");
    Element meta = (Element) nl.item(0); 
    String title = meta.attr("title"); 
    ptext.append("\n" + mNewLinkUrl);

运行它时,我收到一条错误,说明没有为type元素定义attr。 我做错了什么?如果这看起来微不足道,请原谅我。

2 个答案:

答案 0 :(得分:2)

确保Element引用org.jsoup.nodes.Element,而不是其他内容。验证您的导入。同时确保Document引用org.jsoup.nodes.Document。它没有getElementsByTagName()方法。 Jsoup不使用任何org.w3c.dom API。

以下是正确导入的完整示例:

package com.stackoverflow.q4720189;

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

public class Test {

    public static void main(String[] args) throws Exception {
        Document document = Jsoup.connect("http://example.com").get();
        Element firstMeta = document.select("meta").first();
        String title = firstMeta.attr("title"); 
        // ...
    }

}

答案 1 :(得分:0)

据我了解,这里的元素是org.w3c.dom.Element。然后使用meta.getAttribute()代替attr。元素类根本就没有这样的方法。