0

I am trying to parse HTML using jsoup Parser for Android.

If I run my code, I'm unable to get into the for loop; program execution is stopping before the for loop (No error is output).

I had put Log? The same code when I run in normal java project I'm able to get the result?

private class DownloadTwitterTask extends
            AsyncTask<String, Void, List<String>> {
        String content;

        protected List<String> doInBackground(String... urls) {


            try {
                doc = Jsoup.connect("https://twitter.com/someperson/").get();


            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d("Exception", "Exception");

                e.printStackTrace();
            }


            Elements elements = doc.getElementsByAttributeValue("class",
                    "js-tweet-text tweet-text");


            for (int j=0;j<elements.size();j++) {


                Element tmp = elements.get(j);
                String value = tmp.text();

                twitterList.add(value);

            }

            return twitterList;

        }

        protected void onPostExecute(List<String> result) {

            tadaptor = new TwitterLazyAdaptor(INFOActivity.this, result);
            // this.setListAdapter(fadaptor);
            lv1.setAdapter(tadaptor);

        }
    }
4
  • 1
    Maybe elements is just empty? Commented Oct 17, 2013 at 11:31
  • So then there is nothing to loop and you wont get into your for :) Commented Oct 17, 2013 at 11:48
  • But i'm getting the element values ,if i run in normal java project Commented Oct 17, 2013 at 11:51
  • Maybe the twitter link redirects you to a different mobile site :)? Commented Oct 17, 2013 at 12:15

2 Answers 2

1

You can try this one:

doc.select(".js-tweet-text.tweet-text")

The . at the beginning for class, and the . before "tweet-text" for the space.

Update: I compared my solution to yours. It gives the same result.

You can try to set the user agent also. Sometimes a site gives back significantly different html.

        Document doc = Jsoup.connect("https://twitter.com/someperson/")
                .userAgent("Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0")
                .get();
Sign up to request clarification or add additional context in comments.

Comments

0

You should check also if "doc" is empty. If it is empty, maybe there is something wrong with the connection. You can also do selection like this Elements elements = doc.select("p.js-tweet-text tweet-text"); where "p" is tag that you looking and then class attribute. Also have you set <uses-permission android:name="android.permission.INTERNET" /> and where do you execute DownloadTwitterTask

Comments

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.