java - 在 fragment 中的 AsyncTask 中使用 Jsoup 获取网页元素(Android 新手)

标签 java android android-fragments android-asynctask jsoup

我是 Java 和 Android 新手,因此需要您的帮助。 我在我的应用程序中实现了 JSoup,以从网页获取此内容并在 TextView 中显示它(我在 fragment 中操作,但我认为在这种情况下它与标准 Activity 相同)。

<body marginwidth="0" marginheight="0">
<h1></h1>
<p class="testoprezzo">0.5516 </p>
</body>

我只需取 0.5516

我不知道该怎么做。你能帮助我吗? 这是我已经编写的代码:

class fetcher extends AsyncTask<Void,Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        try {

            String value = "https://mywebpage.net/";
            Document document = Jsoup.connect(value).followRedirects(false).timeout(30000).get();
            Element p= document.select ("p.testoprezzo").first();
            ((Globals)getApplication()).setValore(p.text());




        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);


        TextView valore = findViewById(R.id.textView4);
        valore.setText(((Globals)getApplication()).getValore());








    }
}

提前谢谢您!

最佳答案

尽量使用限制性最强的选择器,以免得到不相关的结果。在你的情况下你应该使用 -

Element e = doc.select("p.testoprezzo").first();
String result = p.text();

关于java - 在 fragment 中的 AsyncTask 中使用 Jsoup 获取网页元素(Android 新手),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55431104/

相关文章:

android - 如何在android中更改菜单项 `font style`

java - JVM选项中XX的全称是什么?

java - 不使用 JProgressBar 如何制作进度条?

java - 使用 exoplayer 在 android 9 和 10 上播放流时出错

java - 如何在RecyclerView之上插入图片

java - MapFragment 在 android 中返回 null

java - 让用户点击的地方出现一个按钮

java - 没有找到适合 sort(int[],<anonymous Comparator<Integer>>) 的方法

java - 解析问题 : outstanding network connection.

安卓 fragment : is empty constructor really required?