Wednesday, September 1, 2010

EMT Madrid on my Blackberry Bold

My friend Pablo and I we both live in Madrid, we are both computer science engineers, which implies that neither of us earns enough money to buy a car and pay for gas, which is actually pretty expensive around here. Luckily for us, Madrid has an excellent transport system, in my opinion the best i know of from the cities i have visited. Barcelona,Berlin,London,Milan to name a few. I think this is due to the fact that Madrid has it all. There are excellent  train, subway (the Madrid Metro is the densest metro network in the world) and bus systems, they all so perfectly integrated. Even the Barajas Airport is not far away from the city center.

 All metro and train stations have signs indicating how much time is left till the next wagon arrives. But almost none of the bus stops has this kind of signs. If you think this is unnecessary, think again. Imagine that the next bus isn't coming for the next twenty minutes, you will probably then would like to consider taking the metro or simply walking home.

  If you google a bit you will easily find mobile applications for IPhone and Android which given a bus line and a bus stop number will tell you the corresponding waiting time. For blackberry you will find none. My friend Pablo who apparently has more free time than he is willing to accept decided several months ago he would build his own blackberry application for that purpose. Of course, he forgot that famous phrase about not reinventing the wheel. You will see why.

  Pablo's  initial approach, was to use this public service Waiting Times Bus Madrid , emulate a standard browser request, parse the response ( a whole html page with a lot of useless information) and extract the one line that he needed for his blackberry app. This worked well for a time, until the EMT redesigned its website and changed the http form params.

  Pablo asked for my help knowing i would be interested in getting the application working again. After taking a glance at the web page's source code,  i knew immediately it was going to be very tedious job in case of continuing with Pablo's initial approach. What i did was to download one of the free available Android applications and analyze how it was getting the bus waiting time information.

Step by Step:
  1.  Android apps are  distributed as  .apk files. An .apk file can be opened and inspected using common archive tools such as unzip or unrar.
  2. I found a classes.dex archive which as far as i know contains all the .class files.
  3. I used a dedexer tool.  Dedexer is able to read the DEX format and turn into an "assembly-like format".
  4. In one of the resulting files i found the following
    >> new-instance v4,org/apache/http/client/methods/HttpPost

    >> const-string    v10,"http://195.76.144.242/services/tespera2.asp"

    >>...
  5. >>new-instance v10,org/apache/http/message/BasicNameValuePair const-string >>v11,"linea" >>....
  6. As you can see I could get a "private" url, the method of the request and the params.

I said "private" url because it is a public service after all and I suppose that if I get in touch with the IT department of the EMT I could possibly get to talk to someone who would give me the url and the necessary info.  Or maybe I'll just get lost in bureaucracy or "i have no idea" people.

After getting the URL and the params, the update to Pablo's blackberry app was pretty straightforward. He made it himself. He did not have to parse the response anymore because the new service we are using is designed specially for mobile applications. For that same reason I suppose it wont be changing anytime soon.

If you are interested in building your own blackberry app this is a good start BlackBerry Developer Zone. I already read the guide and found it simple and easy to follow.