jsoup 获取并显示 android 中的 table 内容

jsoup fetching and displaying table contents in android

我有一个 java 程序,它从 This link 中获取 table 内容。我想在 android 中通过在文本视图中显示它来做同样的事情。

下面是我的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 代码,它获取相同的 link。

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();
        }
    }

我可以检索到此人的姓名,但是 table 内容没有显示。

P.S。 java 代码工作正常,我在将它移植到 android

时遇到问题

这应该设置正确的文本而不是打印它:

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();
        }
    }