2

I am completely new to jsoup as of a few hours ago, but i have been reading some beginner tutorials and questions on here but i haven't been able to solve my issue. I have mostly just been guessing at the right combination of css tags but no luck whatsoever.

The element that i'm trying to access is the 'id' tag of this line, i want to store it in a string variable to be more precise.

 <div class="inElm" id="elm11329383">

It is the first of many "inElm" class objects in the document, but i just need the first one's 'id' value. I would post a picture but i'm a noob, here's what i thought would work...

69    Element link = doc.select("div#inElm").first();
70    String idTag = link.attr("id");
71    text.setText(idTag);

Am I close, or do i need to worry about the hierarchy of the document in order to find it? any advice is much appreciated!

here's the whole private Async class:

private class jsouptest extends AsyncTask<Object, Object, Object>{

    @Override
    protected Object doInBackground(Object... arg0) {
        Document doc = null;
        String url = "http://www.pinkbike.com/photo/podlist/";

        try {
             doc = Jsoup.connect(url)
                       .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                       .referrer("http://www.google.com")              
                       .get();
        } catch (NullPointerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (HttpStatusException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Element link = doc.select("div.inElm").first();
    String idTag = link.attr("id");
    text.setText(idTag);
        return null;

    }

}

1 Answer 1

1

inElm is a class attribute, not id. To select elements by class you need to use dot . not #. So try with

Element link = doc.select("div.inElm").first();
// change CSS query here -----^
Sign up to request clarification or add additional context in comments.

4 Comments

AH that makes a lot of sense, but my issue must be deeper. i'll post the logcat.
I am not Android developer, but code from your method works fine for me if I run it from some main method. It seems though that you are getting somewhere NullPointerException which means that you are having reference which holds null and on that reference you are trying to execute some method. If I am not mistaken it happens in line 72 based on log.
The issue is clear, I tried to change the value of a 'view'(textbox) from another class that isn't it's creator, which is apparently against the rules. Thank you for your patience and help @Pshemo
Like I said, since I am not Android developer I can't confirm that problem described in your comment was actually the reason of error so I can't let you edit my answer with this information. If you can't post your own answer because of some kind of time limit then feel free to post it when limit will be gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.