java - 连接到网站 jsoup 项目时出错(Jsoup 类型未定义方法 connect(String))

标签 java android jsoup

我想从网站获取数据到我的 Android 应用程序,所以我使用了 jsoup。

   Document document = Jsoup.connect(url).get(); 

我的项目的这一行出现错误。我三次使用上面的行作为我的要求,但所有三次都使用上面的行,并且所有三行都显示错误消息。 帮助如何消除此错误..

如果有人知道任何其他简单的方法/方式从动态网站数据获取(获取)数据到 Android 应用程序,也请提及这种方式..

    public class Jsoup extends Activity{

    // URL Address
    String url = "http://www.vogella.com/tutorials/Android/article.html";
    ProgressDialog mProgressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Locate the Buttons in activity_main.xml
        Button titlebutton = (Button) findViewById(R.id.titlebutton);
        Button descbutton = (Button) findViewById(R.id.descbutton);
        Button logobutton = (Button) findViewById(R.id.logobutton);

        // Capture button click
        titlebutton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // Execute Title AsyncTask
                new Title().execute();
            }
        });

        // Capture button click
        descbutton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // Execute Description AsyncTask
                new Description().execute();
            }
        });

        // Capture button click
        logobutton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // Execute Logo AsyncTask
                new Logo().execute();
            }
        });

    }

    // Title AsyncTask
    private class Title extends AsyncTask<Void, Void, Void> {
        String title;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(Jsoup.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 document = Jsoup.connect(url).get();
                // Get the html document title
                title = document.title();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Set title into TextView
            TextView txttitle = (TextView) findViewById(R.id.titletxt);
            txttitle.setText(title);
            mProgressDialog.dismiss();
        }
    }

    // Description AsyncTask
    private class Description extends AsyncTask<Void, Void, Void> {
        String desc;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(Jsoup.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 document = Jsoup.connect(url).get();
                // Using Elements to get the Meta data
                Elements description = document
                        .select("meta[name=description]");
                // 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.desctxt);
            txtdesc.setText(desc);
            mProgressDialog.dismiss();
        }
    }

    // Logo AsyncTask
    private class Logo extends AsyncTask<Void, Void, Void> {
        Bitmap bitmap;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(Jsoup.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 document = Jsoup.connect(url).get();
                // Using Elements to get the class data
                Elements img = document.select("a[class=brand brand-image] img[src]");
                // Locate the src attribute
                String imgSrc = img.attr("src");
                // Download image from URL
                InputStream input = new java.net.URL(imgSrc).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(input);

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Set downloaded image into ImageView
            ImageView logoimg = (ImageView) findViewById(R.id.logo);
            logoimg.setImageBitmap(bitmap);
            mProgressDialog.dismiss();
        }
    }

}

最佳答案

您不应该以与现有类相同的方式命名您的类,因为编译器会将简化名称的每个调用连接到当前类,而不是导入的类。例如

class String {
    public static void main(String[] args) {
        System.out.println(String.valueOf("1"));
    }
}

不会编译,因为String.valueOf不会尝试从java.lang.String而是从调用valueOf你的类,并且由于没有这样的方法,你会看到错误说这样的方法未定义。

所以更改你的类(class)名称

public class Jsoup extends Activity{
    ...{
        Document document = Jsoup.connect(url).get(); 
    }
}

更像是

public class JsoupActivity extends Activity{
//           ^^^^^^^^^^^^^
    ...{
        Document document = Jsoup.connect(url).get(); 
    }
}

关于java - 连接到网站 jsoup 项目时出错(Jsoup 类型未定义方法 connect(String)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30154142/

相关文章:

Java如何插入一个0x​​104567910符号?

Java awt 计算器 +/- ,如何将 + 替换为 - : 1st click = "-", 第二次单击 = "+"等等

java - Jsoup:无法选择 tbody id 中的所有行

android - 子标记的 SOAP 请求

java - 我怎样才能直接忽略这个标签来使用 JSOUP 只使用文本信息?

java - 使用 jsoup 提交搜索查询

java - 为什么人们想要学习和/或练习使用带有 Java SE 的服务器,而不是使用 EE 方法?

c# - 在java中使用进程

android - Cordova Android 应用程序从 MobileFirst 8.0 服务器获取 "invalid_client"

java - 如何在 Android Studio 中从 CSV 文件导入时从 SQLite DB 中删除特殊字符?