Mert Kimyonşen
Mert Kimyonşen

Reputation: 47

Android Jsoup Html Parse?

Hi guys I m new in android ı have a proje and ı take data news from website but when ı take data,all news seen in my screen How can I take just the first news and one more thing how can ı put in listview with order.

Web site:

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" class="contentpane">
<tbody><tr>
	<td valign="top" class="contentdescription" colspan="2">
		</td>
</tr>
<tr>
	<td>
	<script language="javascript" type="text/javascript">

</script>
<form action="http://www.izmir.edu.tr/tr/genel-haberler.html" method="post" name="adminForm">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody><tr class="sectiontableentry1"> **FİRST NEWS**
	<td align="right">
		1	</td>
			<td>
		<a href="/tr/genel-haberler/4711-Cocuk-Korumada-Kanita-Dayali-Degerlendirme-ve-Analiz-Cercevesi---IZMIR.html">
			Çocuk Korumada Kanıta Dayalı Değerlendirme ve Analiz Çerçevesi - İZMİR</a>
				</td>
					</tr>
<tr class="sectiontableentry2"> **SECOND NEWS**
	<td align="right">
		2	</td>
			<td>
              <a href="/tr/genel-haberler/4748-Madde-bagimliligi-yuzde-100-tedavi-edilemeyen-bir-beyin-hastaligidir.html">
			“Madde bağımlılığı, yüzde 100 tedavi edilemeyen bir beyin hastalığıdır”</a>
				</td>
					</tr>

protected void onCreate(Bundle savedInstanceState) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
     StrictMode.setThreadPolicy(policy);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.textView1);

    Document doc;
     try {
          // need url
          doc = Jsoup.connect(url).get();
          Elements links = doc.select("table[class=contentpane]");
          Elements row=links.select("tr:gt(0)");
          Elements tds = row.select("td");
          tv.setText(tds.text());
         } catch (IOException ex) {
               tv.setText("Error");
   }   
}

Upvotes: 0

Views: 484

Answers (1)

Danilo
Danilo

Reputation: 1084

Using the code you already have, when you do doc.select(....) it returns a collection of Elements, so you could just do this:

Elements links = doc.select("table[class=contentpane]");
Elements row=links.select("tr:gt(0)");
Elements tds = row.select("td");
tv.setText(tds.get(0).text());

Another way to do this is, only getting the first Element of Elements returned (In one line):

tv.setText(doc.select("table[class=contentpane]").select("tr:gt(0)").select("td").first().text());

Note that I have not tested this code because I don't have JSoup lib right now.

Upvotes: 1

Related Questions