我是 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/