我正在尝试通过 JSOUP 使用 Web 数据更新我的文本视图
出于某种原因,它似乎永远不会使用我试图从中收集数据的 URL 中的表数据来更新我的 TextView。
有什么建议么?
我已经看过很多次了,但我似乎无法发现我做错了什么——但我确信这很简单。
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextView01"
android:text="@string/hello_world" />
</RelativeLayout>
资源:
public class MainActivity extends Activity {
TextView tv;
String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA";
String tr;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
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 {
doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid").first();
Elements tableRows = tableElement.select("tr");
for (Element row : tableRows) {
Elements cells = row.select("td");
if (cells.size() > 0) {
System.out.println(
cells.get(0).text() + "; " + cells.get(1).text() + "; " + cells
.get(2).text() + "; " + cells.get(3).text());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
针对第一个答案进行编辑 - 仍然没有修复:
public class MainActivity extends Activity {
TextView tv;
String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA";
String tr;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
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 {
doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid").first();
Elements tableRows = tableElement.select("tr");
for (Element row : tableRows) {
Elements cells = row.select("td");
if (cells.size() > 0) {
title = cells.get(0).text() + "; " + cells.get(1).text() + "; " +
cells.get(2).text() + "; " + cells.get(3).text();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
我也试过:
public class MainActivity extends Activity {
TextView tv;
String url = "http://sheriff.org/apps/arrest/results.cfm?lname=ABBOTT&fname=LAUREA";
String tr;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
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 {
doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid").first();
Elements tableRows = tableElement.select("tr");
for (Element row : tableRows) {
Elements cells = row.select("td");
if (cells.size() > 0) {
title = cells.get(0).text() + "; " + cells.get(1).text() + "; " +
cells.get(2).text() + "; " + cells.get(3).text();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String title) {
super.onPostExecute(title);
prog.dismiss();
tv.setText(title);
}
}
}