我是Java和Android的新手,所以需要您的帮助。
我已经在我的应用程序中实现了JSoup,以便从网页上获取它并在textview中显示它(我是在片段中操作,但在这种情况下,我认为它与标准活动相同)。
<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());
}
}
先感谢您!
最佳答案
使用Elements获取p标签。
class fetcher extends AsyncTask<Void,Void, Void> {
String txtValue;
@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();
txtValue = p.text();
} catch (Exception e) {
// TODO Auto-generated catch block
txtValue = null;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
TextView valore = findViewById(R.id.textView4);
valore.setText(txtValue);
}
}
请注意,元素和元素不同。根据需要使用它们。
这是所有selectors的列表以及示例。
另请注意:请勿做任何U.I.更改
doInBackground
方法,否则会出现错误。关于java - 在AsyncTask中使用Jsoup在一个 fragment 中获取网页元素(Android Newbie),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55431104/