如何在Android应用中使用Jsoup来显示网站中的某些文本?

时间:2020-06-23 05:02:06

标签: android android-studio jsoup

我想学习jsoup并经历了一些教程和文档,但是它们已经过时了,所使用的功能不再起作用。我了解了我们如何通过以下方式将网站变成文档

文档文档=(文档)Jsoup.connect(“ https://crackwatch.com/”).get();

但是之后呢?

例如,我想从此页面获取文本“破裂”或“未破裂”:https://crackwatch.com/game/detroit-become-human

我该怎么做?请提供代码以及每行的功能。

1 个答案:

答案 0 :(得分:1)

我正在使用JSOUP从PlayStore获取“当前版本”以强制更新应用,您可以从我的代码中获取帮助:

 private class ForceUpdateAsync extends AsyncTask<Void, String, String> {

        private Context context;
        private String currentVersion;
        private AppStartupThreadResponse response;

        public ForceUpdateAsync(Context context, String currentVersion, AppStartupThreadResponse response) {
            this.context = context;
            this.currentVersion = currentVersion;
            this.response = response;
        }

        @Override
        protected String doInBackground(Void... voids) {
            String newVersion = null;
            try {
                //HTML Parsing of the data coming from the url
                Document document = Jsoup.connect("https://play.google.com/store/apps/details?id=com.vassar.unifiedapp.dmaedu&hl=en_IN")
                        .timeout(30000)
                        .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"))
                        .referrer("http://www.google.com")
                        .get();
                if (document != null) {

                    Elements element = document.getElementsContainingOwnText("Current Version");
                    for (Element ele : element) {
                        if (ele.siblingElements() != null) {
                            Elements sibElemets = ele.siblingElements();
                            for (Element sibElemet : sibElemets) {
                                newVersion = sibElemet.text();
                            }
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return newVersion;
        }