我有一个java程序从This链接获取表内容。我想在android中通过在textview中显示它来做同样的事情。
以下是我的java代码
public class VtuFetch {
public static void main(String[] args) throws Exception {
Document doc = Jsoup.connect("http://www.fastvturesults.com/check_new_results/1rn11cs030").
userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36").get();
for (Element table : doc.select("table[id=scell]")) {
for (Element row : table.select("tr:gt(0)")) {
Elements tds = row.select("td:not([rowspan])");
System.out.println("Semester"+" "+"Attempt"+" "+" "+"Total Marks"+" "+" Result"+" "+" Percentage");
System.out.println(tds.get(0).text() + " " + tds.get(1).text()+" "+ tds.get(2).text()+" "+ tds.get(3).text()+" "+ tds.get(4).text());
}
}
}
}
下面是我的Android代码,它获取相同的链接。
private class GetMakrs extends AsyncTask<Void, Void, Void> {
String desc;
Elements tds;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document doc = Jsoup.connect("http://www.fastvturesults.com/check_new_results/1rn11cs030").
userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36")
.get();
// Using Elements to get the Meta data
Elements description = doc.select("meta[name=description]");
for (Element table : doc.select("table[id=scell]")) {
for (Element row : table.select("tr:gt(0)")) {
Elements tds = row.select("td:not([rowspan])");
System.out.println("Semester"+" "+"Attempt"+" "+" "+"Total Marks"+" "+" Result"+" "+" Percentage");
System.out.println(tds.get(0).text() + " " + tds.get(1).text()+" "+ tds.get(2).text()+" "+ tds.get(3).text()+" "+ tds.get(4).text());
}
}
// Locate the content attribute
desc = description.attr("content");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set description into TextView
TextView txtdesc = (TextView) findViewById(R.id.MarksShow);
txtdesc.setText(desc);
mProgressDialog.dismiss();
}
}
我可以检索此人的姓名,但表格内容未显示。
P.S。 java代码工作正常,我有问题移植到android
答案 0 :(得分:0)
这应该设置正确的文本而不是打印它:
private class GetMakrs extends AsyncTask<Void, Void, Void> {
String desc = ""; //<-Important!
Elements tds;
...
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
...
Elements description = doc.select("meta[name=description]");
for (Element table : doc.select("table[id=scell]")) {
for (Element row : table.select("tr:gt(0)")) {
Elements tds = row.select("td:not([rowspan])");
desc = desc + "Semester"+" "+"Attempt"+" "+" "+"Total Marks"+" "+" Result"+" "+" Percentage";
desc = desc + "\n" + tds.get(0).text() + " " + tds.get(1).text()+" "+ tds.get(2).text()+" "+tds.get(3).text()+" "+ tds.get(4).text();
desc = desc + "\n";
}
}
...
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set description into TextView
TextView txtdesc = (TextView) findViewById(R.id.MarksShow);
txtdesc.setText(desc);
mProgressDialog.dismiss();
}
}