user1643284
user1643284

Reputation: 19

Parse data from website using jsoup

i'm creating an android app which is gathering data from www.zamboangatoday.ph, get all the news titles or headers. but i can retrieve only one item can someone check my code.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



            try{

                outputTextView = (TextView)findViewById(R.id.textView1);
                //Document doc = Jsoup.parse(html,"http://www.zamboangatoday.ph");
                Document doc = Jsoup.connect("http://www.zamboangatoday.ph/").get();
                //Elements tag = doc.select(".even h4 a");

                Iterator<Element> iter = doc.select("li.even h4 a").iterator();
                //List<Image> images = new ArrayList<Image>();
                while(iter.hasNext())
                {
                    Element element = iter.next();

                    outputTextView.setText(element.text());

                }

            }catch(Exception e)
            {
                e.printStackTrace();
            }




    }

Upvotes: 0

Views: 499

Answers (1)

ollo
ollo

Reputation: 25340

Not shure what component outputTextView is, but with this code:

outputTextView.setText(element.text());

you'll set the text, and overwrite it with each iteration. So after your loop there will be only the last elements text. If possible use something like outputTextView.append(element.text()); - else use a StringBuilder and set the text after the loop.


outputTextView = (TextView)findViewById(R.id.textView1);
//Document doc = Jsoup.parse(html,"http://www.zamboangatoday.ph");
Document doc = Jsoup.connect("http://www.zamboangatoday.ph/").get();
//Elements tag = doc.select(".even h4 a");

Iterator<Element> iter = doc.select("li.even h4 a").iterator();
//List<Image> images = new ArrayList<Image>();
while(iter.hasNext())
{
    Element element = iter.next();

    /*
     * Append the text - don't overwrite it
     * Node: Maybe you have to add a new line ('\n').
     */
    outputTextView.append(element.text()); 

}

Upvotes: 1

Related Questions