Reputation: 354
Is possible get a String from a webpage in Android? I can see several ways to take all the html content, but is there a way to get ONLY an element? Thanks in advance.
Solved with jsoup by executing this from onCreateView method:
private class Take extends AsyncTask<Void, Void, Void> {
String desc;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect(url).get();
Element link = document.select("h2").first();
String text = document.body().text();
String linkText = link.text();
Log.d(linkText, linkText);
Log.d(text, text);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
Upvotes: 0
Views: 141
Reputation: 7511
yes its possible you have to use a library called jsoup, read more about it here http://jsoup.org/
here's an example
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
Upvotes: 1