Skip to content

Google App Engine – Bringing it Back To Android (4)

December 18, 2011

Hey everyone,

And so we come to the end of our series and we bring it all together and bring it back to Android.

At this point, we have a fully functional, scalable GAE back-end, equipped with cron-jobs that insure your database has the most up to date data, and with servlets put in place so that any platform can make an HTTP request and get your data. All that remains is how to make these HTTP requests from Android, and how to bind this data to the UI once it’s been retrieved.

It’s really very simple and a number of my tutorials in the past have touched on this topic. To start you’ll need to build an HTTP Client which will allow you to make GET/POST requests. The code for this can be found here and in my case I put this code into a class called ConnectionManager. Once you have that, I prefer to build some simple GET/POST wrapper methods, and the code for those look like:

public class GetMethods {

    private static final String LOG_TAG = "GetMethods";

    public static String doGetWithResponse(String mUrl, DefaultHttpClient httpClient) {
        String ret = null;
        HttpResponse response = null;

        HttpGet getMethod = new HttpGet(mUrl);
        try {
            response = httpClient.execute(getMethod);
            Log.i(LOG_TAG, "STATUS CODE: " + String.valueOf(response.getStatusLine().getStatusCode()));
            if (null != response) {
                // helper method to convert HTTP response to String
                ret = getResponseBody(response);
        } catch (Exception e) {
            Log.e("Exception", e.getMessage());
        } finally {
        return ret;

    public static String getResponseBody(HttpResponse response) {
        String response_text = null;
        HttpEntity entity = null;

        try {
            entity = response.getEntity();
            response_text = _getResponseBody(entity);
        } catch (ParseException e) {
        } catch (IOException e) {
            if (entity != null) {
                try {
                } catch (IOException e1) {
                     // error
        return response_text;

For POST requests, code is pretty similar and I talk about those here. Now that we have these nifty generalized GET/POST requests, the rest is super simple. Here’s how to make a GET request for the video games shown in the previous post:

import java.util.List;

import org.apache.http.impl.client.DefaultHttpClient;


public class GetVideoGames {

    private static String URL_BASE = ""

    private static String REQUEST_BASE = "/getVideoGames?type="

    private static DefaultHttpClient httpClient = ConnectionManager.getClient();

    public static List<VideoGame> getGamesByPlatform(String platform) {
        // construct the URL
        String url = URL_BASE + REQUEST_BASE + platform;

        // get the XML response back as a String
        String response = GetMethods.doGetWithResponse(url, httpClient);

        // create a simple String parser
        // here we want to convert XML to a list of VideoGame objects
        List<VideoGame> games = ObjectParsers.parseGameResponse(response);
        return games;


Again, just to reiterate, here we’re simply constructing the correct URL with the specified platform in this case, and then sending it to our new GetMethod wrapper. This GetMethod wrapper will make the GET request and convert the returning HTTP Response object into a more manageable String object. If you want to see what the String looks like, just check out this and pretend like it’s not nicely formatted (i.e. it’s all going to come as one line of characters).

As to how you want to parse that text – this part is really up to you guys (and shouldn’t be too complicated of a task… I’d imagine that if you’re reading this you should have a strong enough grasp of programming to write a String parser). For XML parsing, you can use anything from simple REGEX to more complicated tools like SAX Parsers and XPath (though this might be a little overkill).

If you decide to, instead, return responses in JSON format, then you should definitely use something like JSON in Java which comes with a bunch of nice wrapper methods to help you quickly parse out the fields of each VideoGame object.

Again, just for the sake of completeness though, I’ll show you what I did:

public class ObjectParsers {

    // ad hoc REGEX solution to parsing XML response
    public static List<VideoGame> parseGameResponse(String response) {
        List<VideoGame> games = new ArrayList<VideoGame>();
        String insideContents = RegexParser.regex("<games>(.*)</games>", response);
        String[] contents = insideContents.split("<game>");
        for (String content : contents) {
            if (content.equals("")) {
            String name = RegexParser.regex(">([^>]*)</name>", content);
            String id = RegexParser.regex(">([^>]*)</id>", content);
            String imgUrl = RegexParser.regex(">([^>]*)</imgUrl>", content);
            String type = RegexParser.regex(">([^>]*)</type>", content);
            VideoGame v = new VideoGame(Long.parseLong(id), name, imgUrl, type);
        return games;

And so now that we’ve 1) made the HTTP request, 2) gotten back the request as an HTTP Response object, 3) converted that response object into a String, and 4) parsed the String and converted it into a List of VideoGame objects, all that we need to do is bind these objects to a UI!

Again, this is a topic that I’ve already covered in a previous tutorial, so I won’t make this tutorial unnecessarily long and verbose and instead will ask you to just look at this post. In my previous post, I use a BaseAdapter to create a list which shows a bunch of Contact objects – here we simply need to modify the code a little so that instead of showing a list of Contact objects we’re showing a list of VideoGame objects.

You’ll also notice that one of the fields returned is “imgUrl”, and so one nice thing I do in my application is that alongside the name of the game I show the user a preview image (also taken from Blockbuster). If you want to learn how to convert a URL to an Bitmap then check out this previous post.

As for how you can load a series of images in a list format quickly and without interrupting the user’s application experience (i.e. blocking the UI thread) this is something I’ll leave you guys to think about (as it’s something I have to think about and improve upon constantly!).

Final Result - Simple Activity displaying video game name and image preview.

And that’s it!

Hope you guys enjoyed this series – I’ll follow up shortly with why I feel so passionately about sharing these kinds of full-on application design experiences with everyone.

Happy Coding.

– jwei

10 Comments leave one →
  1. February 18, 2012 1:51 am

    very impressive tutorial, i needed some help with my app and i think you can help me out with it…it based on this tutorial only i wana know after we send a request(data retrieval)from phone to server(app engine) can we send the retrieved data to another phone(not the one which requested).if yes how can we do it?

    • February 19, 2012 8:37 pm

      Hey Rahul – why would you need to do this? Why can’t you just have the other phone make the same request?

      • March 12, 2012 7:15 am

        thanks for replying.. i want to do this for an app i m trying to build
        i’ll give you an overview….

        from 1 android device the user sends data to another android device…
        where the data from sender goes to server where some modification is done and then is send to the receiver…by using C2DM or something like that

        hope u understood what i am trying to do?? can you help??

  2. indie permalink
    March 3, 2012 4:51 am

    Hey I do not see any imports for RegexParser.regex, where do you get that class? which library or implementation?

    • March 13, 2012 10:15 am

      Hey Indie,

      Yes it’s just a simple class that I made which wraps around Java’s Pattern and Matcher classes. Nothing fancy.

      – jwei

      • Indie permalink
        March 13, 2012 1:42 pm

        ah .. okie, thnx mate

  3. maxmaximus permalink
    May 29, 2012 2:04 pm


    would you be so kind to post the android code, cause I really do not understand whats gae code and whats android code(explaining it would also do it 😉 )?

  4. Ivan permalink
    December 23, 2012 1:04 pm

    Hi, Jason, greet work. This is by far the best android GAE example I found on the Internet. I have some problems following the code, I try to test it (and play around with it in Eclipse) but some of the classes are missing (like HTMLNavigator). Can you be so kind to upload the source code somewhere? Thank you very much. Greetings from Zagreb

    • December 23, 2012 7:21 pm

      Hi Ivan,

      The HTMLNavigator code has just been added to this post Many of these posts were written using classes that were stripped out of actual projects/applications that I’ve written so I don’t actually have a single compressable project that I can upload. This is something I’ll keep in mind and work on. I do try to include source on my blog for all the classes I talk about so let me know if there are any other that you can’t find. Thanks!

      – jwei

  5. February 3, 2013 10:49 pm

    Well writing e have a fully functional, scalable GAE back-end, equipped with cron-jobs that insure your database has the most up to date data, this is by far the best android GAE example I found on the Internet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: