Get text from a <p> with specific id in Android using Jsoup - android

I want to fetch some data from my website using Jsoup in my webview. The website is still in development so I can't post any code but here's what I want to achieve:
So the user visits the website where all data I require in the app is loaded onto one page. So I want to fetch all that data as separate strings and use them to fill my table layout. The website has all that I want with each string in a p tag with a unique id.
How can I achieve this? I already have jsoup installed but I can't get my head around how to use it.

Generally if you want to extract element that has id so use select(element_name#id_name)
To extract text that element involves it use .text()
Again show us the html part you want to extract the text from
So try this code
try {
Document doc = Jsoup.connect("your url").get();
System.out.println(doc.select("p#id_name").text());
} catch (Exception e) {
e.printStackTrace();
}

Related

How to display an image in android ImageView from the <img> tag

I have an application which fetches data from a MySQL database. That data contains image urls which I have managed to implement an imageLoader class to display the image using the images url. I have however hit a dead end when it came to displaying an image which is contained in the text fetched and is wrapped in the <img> tag. Kindly assist me in displaying an image contained in the post content. Below is a sample of the data fetched.
This is a sample post, you may choose to continue reading or not. <a href='http://website.com/post'><img src=' http://website.com/post.jpg'></a>
This is a sample post, you may choose to continue reading or not
From the above text, How do I display http://website.com/post.jpg in an android imageview?
If all you want to do is extract the image url from the text returned from the database something like the following will suffice:
int start = s.indexOf("<img src='")+10;
int end = s.indexOf("'",start);
String url = s.substring(start, end));
where s is the string returned from the database
I finally got a perfect solution for my problem. Using jsoup, I am able to get the HTML tags in my string and also I am able to replace the entire HTML segment with whatever content I want. Thanks for your input and hope this answer will help someone sometime.

JSoup Android: Get each results in a separate String

OK, What I want to achieve is to write each result JSoup fetches me in a separate String. Is this somehow possible? I can get the first and last with a function but, yea, then the rest is lost.
right now i have this in my doInBackground:
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the Meta data
Elements titleElement = document.select("h2[property=schema:name]");
// Locate the content attribute
date1 = titleElement.toString();
Log.e("Date", String.valueOf(Html.fromHtml(date1)));
With this i get a list of results which is nice, but i'd like to have every result in a separate String.
Thanks in advance, if you need anything more please ask :)
I read through the documentation carefully again and found this:
element.eq(n).text
The "n" defines which position to get, the .text strips all the html and makes it a readable text

Android: How to parse a HTML site and take only some specific data?

I have a question:
I have a link: http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)
and I wanna take only some specific data from this link and to show in textview in Android.
Is this possible in Android, I mean is there any chance by parsing or I don't know, you can suggest me guys.
For example I just want to take this column Nästa tur (min) from that site.
Regards
JSoup is pretty nice and getting popular. Here's how you could just parse the whole table:
URL url = new URL("http://www.nseindia.com/content/equities/niftysparks.htm");
Document doc = Jsoup.parse(url, 3000);
Element table = doc.select("table[title=Avgångar:]").first();
Iterator<Element> it = table.select("td").iterator();
//we know the third td element is where we wanna start so we call .next twice
it.next();
it.next();
while(it.hasNext()){
// do what ever you want with the td element here
//iterate three times to get to the next td you want. checking after the first
// one to make sure
// we're not at the end of the table.
it.next();
if(!it.hasNext()){
break;
}
it.next();
it.next();
}
If the parsing seems simple enough and you want you could also use regular expressions to find the correct part of the html. Regular expressions will be useful to know at some point anyway. Using some XML/HTML parsing library is the more flexible way to do it (XMLReader for example).

How can you parse HTML in android?

I am making an application for android, and an element of the functionality of the application is to return results from an online search of a library's catalogue. The application needs to display the results of the search, which is carried out by way of a custom HTML form, in a manner in keeping with the rest of the application. Ie, the results of the search need to be parsed and the useful elements displayed. I was just wondering if/how this could be achieved in android?
You would use a Html Parser. One that i use and works VERY well is JSoup
This is where you will need to begin with parsing html. Also Apache Jericho is another good one.
You would retrieve the html document by using DOM, and use the JSOUP Select() method to select any tags that you would like to get. Either by tag, id, or class.
Solution
Use the: Jsoup.connect(String url) method:
Document doc = Jsoup.connect("http://example.com/").get();
This will allow you to connect to the html page by using the url. And store it as the Document doc, Through DOM. And the read from it using the selector() method.
Description
The connect(String url) method creates a new Connection, and get()
fetches and parses a HTML file. If an error occurs whilst fetching the
URL, it will throw an IOException, which you should handle
appropriately.
The Connection interface is designed for method chaining to build
specific requests:
Document doc = Jsoup.connect("http://example.com")
If you read through the documentation on Jsoup you should be able to achieve this.
EDIT: Here is how you would use the selector method
//Once the Document is retrieved above, use these selector methods to Extract the data you want by using the tags, id, or css class
Elements links = doc.select("a[href]"); // a with href
Elements pngs = doc.select("img[src$=.png]");
// img with src ending .png
Element masthead = doc.select("div.masthead").first();
// div with class=masthead
Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
EDIT: Using JSOUP you could use this to get attributes, text,
Document doc = Jsoup.connect("http://example.com")
Element link = doc.select("a").first();
String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""
String linkOuterH = link.outerHtml();
// "<b>example</b>"
String linkInnerH = link.html(); // "<b>example</b>"
You can use XmlPullParser for parsing XML.
For e.g. refer to http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
Being that the search results are HTML and HTML is a Markup Language (ML) you can use Android's XmlPullParser to parse the results.

What is the proper syntax to execute a google web search from my Android 2.2 application?

Working on an Android application, which needs to execute a google web search from within a ListView activity and display the results in that List View.
Such search would always be for the same "static" subject, such as "...bowling alleys in Chicago...", so, the user will not be entering any search criteria.
What is the proper way to accomplish this, please?
I would just URL-encode the search request, create an URL using the official Google Search API and open this in a browser, which would be:
try {
String search = "your search here";
String encodedSearch = URLEncoder.encode(search, "UTF-8");
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com/search?q=" + encodedSearch)));
} catch (UnsupportedEncodingException e) {
// should not happen, maybe add some Log.e() here
}
Have fun! :-)
EDIT: Oh, I just answered too quickly and now I read that you want to display the search results in a ListView. So basically you will need an API key then and use the official search API by Google. More information and documentation here:
http://code.google.com/intl/de/apis/customsearch/v1/overview.html

Categories

Resources