[英]jsoup fetching and displaying table contents in android
I have a java program which fetches the table contents from This link. 我有一个从获取表内容的一个java程序此链接。 I want to do the same in android by displaying it in a textview.
我想通过在textview中显示它来在android中做同样的事情。
Below is my java code 下面是我的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());
}
}
}
}
Below is my android code which fetches the same link. 以下是我的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();
}
}
I am able to retrive the name of the person, but the table contents are not getting displayed. 我可以检索此人的名字,但是表格内容没有显示。
PS The java Code works correct, i am having problem porting it to android
PS Java代码工作正常,我在将其移植到Android时遇到问题
This should set the correct text instead of printing it: 这应该设置正确的文本,而不是打印它:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.