Android Jsoup Html 解析?
Android Jsoup Html Parse?
大家好,我是新来的 android 我有一个项目,我从网站上获取数据新闻,但是当我获取数据时,我的屏幕上看到的所有新闻我怎样才能只获取第一个新闻和另一个新闻我怎样才能按顺序放入列表视图。
网站:
<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");
}
}
使用您已有的代码,当您执行 doc.select(....) 时,它 returns 元素的集合,因此您可以这样做:
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());
另一种方法是,只返回第一个元素的元素(在一行中):
tv.setText(doc.select("table[class=contentpane]").select("tr:gt(0)").select("td").first().text());
请注意,我还没有测试这段代码,因为我现在没有 JSoup 库。
大家好,我是新来的 android 我有一个项目,我从网站上获取数据新闻,但是当我获取数据时,我的屏幕上看到的所有新闻我怎样才能只获取第一个新闻和另一个新闻我怎样才能按顺序放入列表视图。
网站:
<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");
}
}
使用您已有的代码,当您执行 doc.select(....) 时,它 returns 元素的集合,因此您可以这样做:
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());
另一种方法是,只返回第一个元素的元素(在一行中):
tv.setText(doc.select("table[class=contentpane]").select("tr:gt(0)").select("td").first().text());
请注意,我还没有测试这段代码,因为我现在没有 JSoup 库。