2014-01-07

This is for a university project I'm working on, so the less code the better. I'm more interested in understanding the key concepts

I built a basic Client/Server web app using the Play Framework (it is a framework which uses the MVC pattern)

So right now, when a user goes to the web page and selects a command, the command is appropriately routed to the correct Controller method, which fetches data from the DB and manipulates it, then returns it to the user/client. The data that is returned can be any object (arraylist, hashmap, you name it!). Nice and simple. My "view" will then display the data as appropriate.

Now, I am creating a native android app that is virtually a clone of the web app. They will both have the same functionality, etc. Since the logic already exists on the server, and the entire architecture is in place, it seems to me that the biggest difference between the web and mobile app will be how the data is transmitted.

As described above, the data was sent back and forth easily in the web app. However, for the mobile app, I have decided to communicate with the server (with JSON) using the HTTP Post request/response functionality in Java.

I have set up a basic example to make sure that the functionality works as intended... I am able to send JSON from the android app to the server, and retrieve some data back from the server. Good

Now, my question is: How should I approach and manipulate my current server/controller so that it can serve both the web and mobile apps? Allow me to elaborate... I currently have a controller that is:
1) fetching data and manipulating it
2) sending data to web app client
Now, the "easy" way out (and I don't want to do this, because it's not right and will affect my grade negatively) would be to create a new controller, Controller2, which is an exact copy of the first controller, but will defer because it will render/compose the data (to be returned) as JSON, then send it back. Too much code duplication though, and that's bad.

So now, how exactly can I use the Controller logic that exists, but be able to serve it to the web app and the mobile app? I need some guidance, because the only idea that I've been able to muster up is to have 3 controllers (1 logic controller, 1 web-app controller, 1 mobile controller), and the web-app/mobile controllers will call the logic controller, get the data that they need, then assemble it appropriately before delivering it to the web-app or mobile phone (the web-app controller would get the data back and then deliver it to the client, as it does currently;; while the mobile controller would get the data, and render it to JSON before sending it off to the phone).

Please help, thank you!

Show more