Blogs

SpringSource Blog

Spring into Mobile Application Development

Keith Donald

At SpringOne2gx we announced exciting new initiatives in the areas of social media and mobile application development. A few weeks ago, Craig Walls released Spring Social. Today, Roy Clarkson released Spring Mobile and Spring Android. In this post, I'd like to highlight these projects and share how Spring aims to simplify mobile application development.

Choices in Mobile Application Development

If you attended SpringOne2gx this year, you've seen Greenhouse, an app we built for our community that also serves as a reference and driver for Spring technology. Craig showed you some of the social elements of Greenhouse, such as the ability to connect your account with Twitter and Facebook. There are also a number of mobile elements. Specifically, Greenhouse doubles as a mobile web app, and sports native Greenhouse for iPhone and Android clients.

Like many organizations today, we had to answer the basic question of "what mobile platforms to target?" In the end, we chose to invest in a native experience for iPhone and Android users, while also developing a cross-platform mobile web app. Our decision to go native was driven by the fact the app is consumer-oriented, and a large number of prospective consumers (application developers) own iPhone and Android devices. At the same time, the mobile web app aims to provide a good baseline experience that works cross-platform, something that's possible today with the rise of WebKit and HTML 5.

From our development work, grew Spring Framework contributions: first, a Spring Mobile project that provides extensions to Spring MVC for developing mobile web apps; and second, a Spring Android project that supports the development of native Android clients that communicate with Spring-based back-ends. I'll take you through each of these projects in turn.

Spring Mobile

The first problem we tackled was designing a web app that was pleasant for mobile visitors to use. While a smartphone may have a capable web browser, it still has a small screen and that needs to be accounted for. There are essentially two approaches to dealing with this problem:

  1. Detect the device that originated the web request and serve a separate site to mobile devices.
  2. Serve a single site, but progressively enhance it for desktop users by using CSS 3 media queries and JavaScript.

You can find examples of both techniques in the wild today; for example, the conference site Lanyrd enhances through client-side detection, while SpeakerRate uses server-side detection that redirects mobile visitors to a separate site (see for yourself by installing the Firefox User Agent Switcher and setting your user agent to iPhone).

In Greenhouse, we started with server-side detection. Specifically, we aimed to apply a different page layout if the device was a mobile device. Out of this grew a general purpose "Device Resolver Abstraction", which is the defining feature of Spring Mobile 1.0.0.M1. Some highlights of this feature include:

  1. A HandlerInterceptor that uses a DeviceResolver to detect the Device that originated the current HttpServletRequest.
  2. The ability to inject the detected Device into @Controller methods and view templates to vary logic by device type.

Below are some usage examples from the Greenhouse codebase. First, have a look at the interceptor definition from /WEB-INF/spring/appServlet/servlet-context.xml:

<interceptors>
    <!-- On pre-handle, detect the device that originated the web request -->
    <beans:bean class="org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor" />
</interceptors>

Now, a JSP template that conditionally renders some content if the device is not a mobile device:

Please try again<c:if test="${!currentDevice.mobile}"> or <a href="<c:url value="/signup" />">sign up</a></c:if>.

Finally, a Tiles-based page layout that changes if the device is a mobile device:

    <definition name="page" templateExpression="/WEB-INF/layouts/${currentDevice.mobile ? 'mobile/' : 'standard/'}page.jsp" />

In addition to these features, Spring Mobile 1.0.0.M1 ships:

  1. A HandlerInterceptor that redirects mobile visitors to another URL. This is useful if your mobile site truly is a separate application. Consider Flickr, for example, which will redirect you to m.flickr.com if you access www.flickr.com from your phone.
  2. A DeviceResolver implementation that delegates to WURFL for device detection. WURFL provides a large database of devices and their capabilities. It is useful when you need to know more about the Device that originated the request, such as its specific screen size, manufacturer, model, preferred markup, or other capabilities.

Recently, we have also begun exploring the use of CSS 3 media queries with JavaScript to perform client-side detection. This approach has the advantage of not requiring special server-side handling unless you aim to vary the semantic content you send to the device, and not just optimize the style. Not all browsers support media queries, so they may not be an option for you, but if you're targeting smartphones with WebKit-based browsers you should be fine. In general, the approach of first designing your app for mobile, then progressively enhancing it for desktop feels quite elegant to me.

Spring Android

For the Android client, a different set of challenges emerged. We needed to exchange data with the server over HTTPS via REST, and since that data is user-specific we require the user to sign in. Rather than use Basic Auth, where we would need to store username and password credentials on the device itself, we opted for OAuth.

OAuth is an emerging standard that provides a token-based authorization scheme. Essentially, a username and password is traded for an access token, and the access token is used to make requests for protected resources. This means you only need to store the access token with the device for "remember me" functionality. Furthermore, we chose to implement the sign in process whereby the client takes you to the server's web site for connection authorization. In this way, the client never sees your username and passsword, which is important if you allow third-party clients to be developed against your API (which we also want to encourage). Finally, in the event a user's phone is stolen or otherwise compromised, an access token can be invalidated without the risk of username and password compromise.

Out of this work, came the desire to get specific modules of the Spring Framework working in an Android environment. Specifically, we aimed to use RestTemplate for the REST API calls, and Spring Security for the OAuth client.

I'm pleased announce that the first milestone of Spring Android ships an "Android-ready" RestTemplate. We're using it in Greenhouse, and we encourage you to use it as the REST client in your own Android applications. Check out the example usage below:

RestTemplate restTemplate = new RestTemplate(new CommonsClientHttpRequestFactory());
Event event = restTemplate.getForObject("https://myapp.com/event/{name}", Event.class, "springone2gx");

In future milestones, you can expect more of the Spring Framework to be supported in an Android environment, such as the Spring Security OAuth client.

Getting Started

The best way to get started is to see it live by walking through the Greenhouse app in your own local development environment. As a reference, Greenhouse uses the Spring Mobile and Spring Android projects, as well as Spring MVC, Security, Social, and Integration. The project page provides a guide that shows you how to get the web app, iPhone client, and Android client all running in your local environment in minutes.

If you have questions about features, roadmap, or just want to have a discussion with the development team, please do visit our community forums. We value your feedback.

Summary

I'm very excited about all the new initiatives we've got going, and especially what we're doing in the social and mobile space. The first milestones of these projects are just the beginning. I encourage you to get involved in the projects that are useful to you and help us make them the best they can possibly be.

Similar Posts

Share this Post
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Slashdot
  • Technorati
  • Twitter
 

43 responses


  1. Wow, I've been thinking about creating my own springy kinda framework as soon as I had the time, but you guys already have something available now. This imo is awesome news.

    Best wishes with the project, I'm sure it will become another standard way of resolving issues in a java environment.


  2. This is great news ! Looking forward to all the future releases.

    By the way https://greenhouse.springsource.org/ is down at the moment :(

    cheers !
    rohit


  3. Rohit,

    The Greenhouse public instance appears to be running fine from our end. It's monitored by Hyperic, so we will receive an alert if the app is not available. I'm just about to load in the data for the upcoming Rich Web Experience conference, so you'll see that in there soon as well.

    Thanks for the feedback!

    Keith


  4. Keith:
    Thanks for your reply. I am still not able to to do anything with greenhouse. This is what happened yesterday and this is what is happening now –

    1) Hit the URL https://greenhouse.springsource.org
    2) It comes up
    3) click on Register Me link (https://greenhouse.springsource.org/signup)
    4) It breaks – comes up with
    "HTTP Status 500 -type Exception report message
    description The server encountered an internal error () that prevented it from fulfilling this request.exception
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ava.lang.NumberFormatException: For input string: "5.5"
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)"

    5) Now try accessing the main URL again (step 1 above which worked earlier), it also breaks now – with the same exception trace as shown in point 4 above.

    PS: I have taken the screenshots – let me know if you want me to email them to you.

    cheers !
    rohit


  5. Rohit,

    Unfortunately, I am unable to reproduce your problem on either Firefox, Safari, or Chrome browsers. We also have had a number of sign ups today and yesterday.

    Would you mind creating a JIRA that describes your environment, including your browser version, and contain the exact reproduction steps, full exception stack trace, and screenshots. The JIRA is definitely the right place to report and discuss specific issues with the app and we'll take a more detailed look from there.

    Thanks!

    Keith


  6. Wow, just in time when I'm becoming Android developer… Thanks! :)


  7. Keith:
    Done ! GREENHOUSE-486 is the defect number and here is the link https://jira.springframework.org/browse/GREENHOUSE-486

    For what it's worth I have also uploaded the screenshots.

    PS: waiting to get registered :(

    cheers !
    rohit


  8. Hi Keith,

    I have currently been working on a library called AsyncAndroid which allows user to communicate with a Remote server asynchronously using Android.

    It makes use of HttpClient and the Gson library to convert JSON response to POJO's.

    I'm currently finalising the library and trying to decouple the response from the updating of the UI so it is still somewhat of a work in progress but it may be worth the Spring guys taking a look on the implementation.

    The project has been influenced by my usage of Spring and its automatic Json building views as well as my use of Google Web Toolkit and Asynchronous communication.

    The project is hosted on Google Code at:

    http://code.google.com/p/asyncandroid/

    Hope it might help somewhat,

    James


  9. great post. i wanna share also a spoon-feed tutorial on Spring MVC

    http://www.adobocode.com/spring/a-spring-web-mvc-tutorial

    hope it will help people!


  10. awesome. i’m looking for this kidda staffs. i am a open source and spring lover. all the best for this mobile project.


  11. hi, Keith,

    when i run android client, found following error

    [2010-11-23 17:19:06 - Greenhouse for Android] Failed to install Greenhouse for Android.apk on device 'emulator-5554!
    [2010-11-23 17:19:06 - Greenhouse for Android] (null)
    [2010-11-23 17:19:07 - Greenhouse for Android] Launch canceled!
    [2010-11-23 17:28:36 - Greenhouse for Android] warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
    [2010-11-23 17:28:37 - Greenhouse for Android] warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
    [2010-11-23 17:31:24 - Greenhouse for Android] ——————————


  12. Is it possible to define a different controller to handle the request depending on if it's a mobile device or not? In terms of separation of concerns, I'd have though that this approach would be more suitable?

    i.e. what you display to the user is likely to be different depending on the device, there may be little or no cross over in terms of data required in the model. You *could* continue to populate the model with all data (mobile and non) regardless, but this seems wasteful. Alternatively you could introduce logic into the controller to choose what data is populated depending on the device, but then this logic will be scattered throughout every controller. Either way you still need some logic to decide which view to display in each controller or view.

    It would be nice if there was an attribute of the @RequestHandler where you could specify a device and then the dispatcher dispatches accordingly.

    This would also improve the testability and readability of the resulting code as you'd end up with two controllers each dealing with different situations, rather than one controller that tries to handle it all


  13. Hi Keith,

    Greenhouse webapp error :

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ava.lang.NumberFormatException: For input string: "5.5"
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)

    appears to all those from India cause we are 5 hours and 30 mins (5.5) ahead of GMT. Code takes in a number and not float.

    Thanks,

    Arindam


  14. Hi Keith,

    I need one concern from you.
    Our web application is running on weblogic server.
    Our plan is to upgrade the WEB applications to WAP enabled application so the user can also view the same in mobile devices.

    So can you please shed light how come we can come up with solution using Spring for this requirement?

    Our intention is to create an asset/tool so that it can promote all our WEB applications (running on Weblogic and JBoss) to WAP.

    Appreciate for your valued reply.

    regards
    SK


  15. This is great news ! Looking forward to all the future releases.
    Best wishes with the project.


  16. great news , I have join a organizational as a Mobile Application Development and this post help to me . Thanks


  17. Thanks…..for the post

    The Taj Mahal App : One of the World's TOP human-built WONDERS arrives on iPad : DOWNLOAD TODAY


  18. Very informational write up… Thanks for valuable info


  19. Great information thanks for sharing this with us.In fact in all posts of this blog their is something to learn . your work is very good and i appreciate your work and hopping for some more informative posts . Again thanks


  20. Its a very informative stuffs available here in this blog and lots of clearification had been made on iPhone as well as Android users point of view, which could be beneficial to them, great work keep it on


  21. Couple notes on M3 release:

    The class org.springframework.mobile.device.LiteDeviceResolver is out of date. It is not resolving some modern mobile devices. Do you have any plans for an update?

    Developers can extend the LiteDeviceResolver fixing the device resolution logic. However, I think, it would be the best making it configurable…

    Regards.


  22. The mobile industry is so vast. Everything is so possible on computers and mobile phones right now.


  23. Very good post helps a lot. Good luck keep it up.

    http://www.blazemedea.com


  24. Hi this is jack Dowson I just read your blog about Web Design, such a great list of blogs about web design and I agree with your list. These are really resourceful and top related blogs on web designs. I was searching about web designing and I found your website with useful information, thanks for information.


  25. This very useful post, thanks for sharing it. The Mobile Application industry offers endless opportunities.


  26. Thanks for giving information about this field. now with HTML 5 browsers are also changing very rapidly for fully support to HTML 5.
    Online Free videos


  27. Excellent details thanks for discussing this with us


  28. Thanks For sharing your Knowledge. Mobile phone Development Companies always giving surprises with new applications.


  29. Is there a book for learning Spring-Android? Can somebody post the link where I can purchase one?

    Thanks


  30. Thanks for this great article! Definitely want to get more into the mobile computing field as it continues to expand.


  31. thank you for sharing the information…

    Samanyu is one place for all solutions regarding mobile applications


  32. Your mobile website visitors gain the following benefits…

    - They get quick access to directions to your business.

    - They can call you with one click on their mobile phone.

    - They can easily add your business phone number to their
    mobile phone address book.

    - They can easily bookmark your mobile website address for
    fast future reference.

    Very few of your competitors are using the new mobile web technology. Get in now and start leading the pack. If your company does not have a mobile presence, you risk losing ground to your competition that do. You also risk failing to connect with users, and miss out on opportunities to extend and strengthen your brand.
    http://gomobile.searchbarbadosonline.com/


  33. Improvements include a smoother and more responsive UI across the system, a home screen that automatically adapts to fit your content, a powerful predictive keyboard, richer and more interactive notifications, larger payload sizes for Android Beam sharing and much more. For a lowdown on what’s new, head over to the Jelly Bean platform highlights.


  34. Nice blog!…….Our team has successfully mapped the business process requirements of all our clients consistently.
    SBL focuses on delivering the Best, Cost-effective solutions, and Value added services on-time to meet a wide range of requirements, in order to help clients acheive greater returns on investments for their technology initiatives.
    SBL combine our mobile application development expertise, innovative research techniques and new approaches together to provide you the best solution.If you would like to know more about the different services we offer please feel free to contact us
    SBL
    1B, 1st Floor,
    Leela Infopark – Phase – II
    Kakkanad, Cochin,
    India – 682030
    Tel: 91 (0) 859 023 3999
    Fax: 91 (0) 484 2784056


  35. Thanks for this great and informative post. Application market is always providing something new to the customers and therefore mobile application development industry is booming today. Great to see Spring into mobile apps development.


  36. Thanks for sharing this wonderful knowledge with us.I'll look forward to it.


  37. Thank you for the informations about Mobile Application Development! Looking forward to more of your articles.


  38. Thanks for this great and informative post. Looking forward to more of your articles.


  39. I think more people need to read blogs like this. Its so important to know how to construct a great blog to get people interested and youve done just that. assignment writing assistance uk | who to get assignment | speech assistance uk


  40. Such a interesting blog you share us, keep it up..
    gatwick airport valet parking


  41. Nice site, how do I join your mailing list? I’m new to blogger and here’s my domain


  42. Nice site, how do I join your mailing list? I’m new to blogger and here’s my web domain


  43. i wish i had knowledge to develop applications for mobile this is what i was wishing for until i found a mobile application that suited all my publishing needs PressPad http://www.presspadapp.com with this great information that you have laid out here i now know some basics in this industry.

11 trackbacks

Leave a Reply