Skip to content

Google App Engine – HTTP Requests (3)

November 27, 2011

Hey everyone,

Hope everyone is sufficiently stuffed from the holidays… I know I am.

But yes to recap, so far we’ve in this series we’ve seen how you can use Google App Engine to set up a back end for your Android application, which includes writing all the wrapper functions for inserting/updating/removing data from the JDO database back end. Then, we looked at powerful automated methods for retrieving data – incredibly important for people who need a strong initial data set in their application before going live with it. And lastly we tied the two together by showing you how to go and retrieve the data and then batch insert it into your JDO database.

And so, at this point, everyone should be able to go to their Google App Engine dashboard and see the following:

VideoGame Database from GAE

As well as a list of your active CRON jobs:

List of active CRON jobs on GAE

Now, moving on. At this point we need to set up our HTTP GET/POST requests – namely we need to be able to call a URL which hits your Google App Engine server and performs some kind of function (typically this will be retrieving some data based on the parameters you pass into the URL, and then returning it in one of two formats – either XML or JSON). This is done through defining an HTTP Servlet and the code is actually quite simple:

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import app.db.VideoGameJDOWrapper;
import app.types.VideoGame;

public class GetVideoGames extends HttpServlet {

    private static final long serialVersionUID = 1L;

    // note this is a simple HTTP GET request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // from the URL request, retrieve the parameter with name 'type'
        String platform = request.getParameter("type");

        // pass this into our JDO wrapper which then goes and retrieves the games with specified platform type
        List<VideoGame> games = VideoGameJDOWrapper.getGamesByPlatform(platform);

        // convert the list of VideoGame objects into a String with XML format (can be JSON as well)
        String ret = GamesToXMLConverter.convertGamesToXML(games);

        // set the response type to be XML
        response.setHeader("Cache-Control", "no-cache");

        // this is the response that you write back to the user


The GamesToXMLConverter class is also just a wrapper class that given a list of VideoGame objects, will strip out the fields of each and translate them into an XML string. Again, pretty simple code, and I’m sure someone out there has a much cleaner way of doing this object to XML conversion… but this has always worked fine for me:

public class GamesToXMLConverter {

    public static String convertGameToXml(VideoGame g) {
        String content = "";
        content += addTag("name", g.getName().replaceAll("&", "and"));
        content += addTag("id", String.valueOf(g.getId()));
        if (g.getImgUrl() != null) {
            content += addTag("imgUrl", g.getImgUrl().getValue());
        content += addTag("type", VideoGameConsole.convertIntToString(g.getConsoleType()));
        String ret = addTag("game", content);
        return ret;

    public static String convertGamesToXML(List<VideoGame> games) {
        String content = "";
        for (VideoGame g : games) {
            content += convertGameToXml(g);
        String ret = addTag("games",content);
        return ret;

    public static String addTag(String tag, String value) {
        return ("<" + tag + ">" + value + "</" + tag + ">");


The last step in this process is simply defining this HTTP servlet in our Google App Engine project. This happens in the project’s /war/WEB-INF/web.xml file, at which point you’ll insert the following code snippet into that file:

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="" version="2.5">



Note that you must pass in the path to your HTTP Servlet class within the “servlet-class” tags (in my case this was in the app.requests package of my project). The URL pattern will simply tell you the URL path that you need to send your HTTP request to and will look something like:


And so for me, it looks like:

Sample HTTP GET request for getting Video Games

Feel free to hit the following URLs yourself to get a better idea of what’s going on:

Xbox Request

PS3 Request

Wii Request

And for those that are wondering, by using a browser like Chrome or Firefox (not sure if IE does this) the XML should automatically get formatted nicely (as shown in my sample image).

Any who, that’s it for now! Next step is fairly simple – we now have our HTTP request set up, so all we have to do is hit it from our Android client to retrieve a nicely XML-formatted string of data, at which point we can parse it, cache it, bind it to the UI, etc.

Hope this was helpful – happy holidays and like always, happy coding.

– jwei

4 Comments leave one →
  1. November 28, 2011 11:36 am

    It is perfect time to make some plans for the future and it’s time to be happy. I have read this post and I really really like the topic about gadgets.

  2. Mike Breytenbach permalink
    December 20, 2011 7:22 am

    Awesome, thank you


  1. Google App Engine – HTTP Requests (3) « Think Android |
  2. Google App Engine – HTTP Requests (3) « Think Android | industry, blog, iphone, app, creative, games, programming, project, various, criminalminds

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: