Android html download and parse error

I am trying to load an html file using ul pages. I am using Jsoup. This is my code:

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);

When I start, I get an error message indicating that attr is not defined for the type element. What have I done wrong? Forgive me if this seems trivial.

+3
source share
2 answers

Be sure Elementto relate to org.jsoup.nodes.Element, and not to anything else. Check import. Also make sure that Documentapplies to org.jsoup.nodes.Document. He has no method getElementsByTagName(). Jsoup does not use any of the APIs org.w3c.dom.

Here is a complete example with the correct import:

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"); 
        // ...
    }

}
+2
source

, org.w3c.dom.Element. meta.getAttribute() attr. .

0

Source: https://habr.com/ru/post/1784629/


All Articles