我有一个基于 android jsoup 的应用程序,用于从 HTML 表中提取数据,但是我无法从以下 url 中提取数据:
http://sheriff.org/apps/arrest/results.cfm?lname=&fname=
我只需要一些帮助来弄清楚如何解析这个特定表中的数据。
我知道我需要在这里更改一个参数:
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for (Element element : tableHeader.children()) {
aa.add(element.text().toString());
这是我第一次通过 java/androind 提取 HTML 数据,我不确定它是如何完成的。
非常感谢任何输入。
资源:
public class MainActivity extends Activity {
Context context;
ArrayList<String> aa = new ArrayList<String>();
ListView lv;
final String URL = "http://example.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.listView1);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for (Element element : tableHeader.children()) {
aa.add(element.text().toString());
}
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,aa);
lv.setAdapter(adapter);
}
}
}
HTML:
<table class="datagrid">
<tr>
<th>User Name</th>
<th>Date</th>
<th>Time</th>
<th>Location</th>
</tr>
<tr>
<td><a href="redirector.cfm?ID=c4e7a7ea-0832-4cdb-9b38-4cbdde8c07bc&page=1&&lname=&fname=" title="501207593">501207593 </a></td>
<td>LASTNAME, FIRSTNAME </td>
<td>M </td>
<td>Location1</td>
</tr>
<tr>
<td><a href="redirector.cfm?ID=6dfb8f0b-949a-49a1-b3bf-b361544ee5d8&page=1&&lname=&fname=" title="501302750">501302750 </a></td>
<td>LASTNAME, FIRSTNAME </td>
<td>M </td>
<td>Location2</td>
</tr>
<tr>
<td><a href="redirector.cfm?ID=b638597e-0319-4eea-a2d4-d763d43125eb&page=1&&lname=&fname=" title="531201804">531201804 </a></td>
<td>LASTNAME, FIRSTNAME </td>
<td>M </td>
<td>Location3</td>
</tr>