使用Jsoup在TextView中显示单元格表数据

时间:2015-02-08 05:39:19

标签: android html css jsoup

我希望在滑雪胜地过去24小时内在TextView雪中展示。我使用了CSS路径并尝试了其他方法,但没有任何事情TextView没有显示任何内容。

网页:http://www.arizonasnowbowl.com/resort/snow_report.php

CSS路径:#container> div.right> table.interior> tbody> tr:nth-​​child(2)> td.infoalt

private class Description extends AsyncTask<Void, Void, Void> {

    String desc;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(Snowreport.this);
        mProgressDialog.setTitle("Snow Report");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup.connect(url).get();
            Elements elms = document.select("td.infoalt");
            for(Element e:elms)
                if(e.className().trim().equals("infoalt"))
                //^^^<--trim is required as,
                // their can be leading and trailing space
                {

                    TextView txtdesc = (TextView) findViewById(R.id.snowp24);
                    txtdesc.setText((CharSequence) e);


                }



            mProgressDialog.dismiss();

            } catch (IOException e1) {
            e1.printStackTrace();
        }

        return null;
    }

2 个答案:

答案 0 :(得分:0)

您的选择参数可能不正确String。通过右键单击Chrome浏览器,可以通过“检查网页的元素”找到用作Document.select()参数的正确选择。

以下代码可能会为您带来更好的结果:

final Elements tableElements = response.parse()
                    .getElementsByClass("info")
                    .select("td");

for (Element element : tableElements) {

    String string = element.getElementsByClass("infoalt").text().trim()

    Log.d("Jsoup", string);

}

祝你好运,编码愉快!

答案 1 :(得分:0)

代码:

Element div = doc.getElementById("contentinterior");
Elements tables = div.getElementsByTag("table");
Element table = tables.get(1);

String mSnow = table.getElementsByTag("tr").get(1).getElementsByTag("td").get(1).text();