Issue
I'm new to Java and Android so I need your help. I've implemented JSoup in my app to take this from a webpage and show it in a textview (I'm operating in a fragment but i think it's the same as a standard activity in this case).
<body marginwidth="0" marginheight="0">
<h1></h1>
<p class="testoprezzo">0.5516 </p>
</body>
I've to take only 0.5516
I have no idea on how to do it. Can you help me? This is the code I've already written: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());
}
}
Thank you in advance!
Solution
Use the Elements to get the p tag.
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);
}
}
Please see that Elements and Element are different. Use them as per your need. Here is the list of all selectors along with example.
Also note: DON'T DO ANY U.I. CHANGE in doInBackground
method, or else you will get an error.
Answered By - Masoom Badi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.