• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to obtain and parse data from a website?

ang87

Lurker
Hi all,

I want to create an app that displays flight information such as: arrival/departure info, canceled flights, etc. I don't know how to query the data from the website in order to display it in the app. Can you please give me a starting point? Please help me

Thanks in advance
 
Create a URLConnection and obtain an InputStream (recommend creating a BufferedReader from this for easier reading), then read and parse the HTML file. If it is a PHP or jsvascript file, then youll need to qudry the results, which requires you know the format of the data ahead of time.
 
Thanks for the answer. I'm not sure if I understood correctly. What do you mean by : "which requires you know the format of the data ahead of time"
 
If its a simple html file, then its just marking up and displaying text, so you can just parse text itself. PHP is primarily used for database manipulating and is not actually displaying anything. It does, sometimes however, return some data. you can catch this data and--provided you know the format of the data--parse the response. Javascript files are Web apps, so they are more along the lines of programs than websites. I cant really think of any situation where you would need any info from a Javascript file though.
 
Maybe you can provide a link to the website you want to parse? :)

It is definitely possible, but it depending on the data format you have to to more or less work.

For example, if the website is formatted as HTML, you will have to search for the interesting pieces by hand and strip all the markup that tells the browser how to lay it out for humans to read.
If it is served as HTML only, the simplest thing might be to use regular expressions that search through the HTML code for you and pick just the pieces you like.

If the website is served as valid XHTML, you might have an easier time as Android ships with a variety of XML parsers so you can use them and don't have to parse the HTML string yourself. See the XML PullParser documentation for one of the XML parsers that come with Android.
 
Back
Top Bottom