0

我正在尝试实现 JSOUP 查询,但是我收到一条错误消息,指出“无法解析 doc”和“无法将 doc 解析为变量” 我知道我需要先调用 doc 才能使用它 我是只是不知道怎么做——这是我第一次用 JSOUP 构建解析器——我相信这很简单——我只需要一个快速指针。

public class MainActivity extends Activity { 

    TextView tv;
    String url = "http://microsoft.com";
String tr;

    @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);
        }
    }
}
4

1 回答 1

0

您正在尝试使用尚未声明的变量。就像你做的那样

TextView tv;

您将必须声明变量 doc。

Document doc;

不要忘记导入以下包org.jsoup.nodes.Document

于 2013-10-06T21:03:40.773 回答