0

在尝试其他用户在以下 SO 帖子中发布的建议后,我收到 3 个错误:

无法使用简单 JSOUP 示例解析网站表数据

这是我第一次使用 JSoup 构建任何东西,因此非常感谢任何提示/技巧或我应该知道的事情。

错误:

Type mismatch: cannot convert from Elements to Element  
The method select(String) in the type Element is not applicable for the arguments (Element)
tr cannot be resolved to a variable 

资源:

package com.example.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tv;
    final String URL = "http://exampleurl.com";

    @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 {
                Document doc = Jsoup.connect(params[0]).get();
                Element tableElement = doc.select(".datagrid");
                Element th = doc.select(tr).first;
                Element firstTh = th.select(th).first();
                title = firstTh.text();
        }  catch (IOException e) {
                e.printStackTrace();
            }
            return title;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            prog.dismiss();
            tv.setText(result);
        }
    }
}

修订版 1:

public class MainActivity extends Activity {

    TextView tv;
    final String URL = "http://exampleurl.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 {
                Document doc = Jsoup.connect(params[0]).get();
                Elements tableElement = doc.select(".datagrid");
           //     Elements th = doc.select(tr).first;
                Elements th = doc.select(".attribute").first();
                title = firstTh.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

根据 javadoc,select返回一个ElementsObject 而不是Element.

Elements tableElement = doc.select(".datagrid");

select接受String文字而不是元素,因此String需要 a 作为其参数。方法也需要括号。作为first一种方法 - 它需要括号

Element th = doc.select(".yourattributehere").first();
                             ^              ^----- add parenthesis
                             |--provide valid selector query
于 2013-10-02T16:08:05.893 回答