Google App Engine – HTTP Requests (3)
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:
As well as a list of your active CRON jobs:
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.io.IOException;
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.services.GamesToXMLConverter;
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.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
// this is the response that you write back to the user
response.getWriter().write(ret);
}
}
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="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>getVideoGames</servlet-name>
<servlet-class>app.requests.GetVideoGames</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getVideoGames</servlet-name>
<url-pattern>/getVideoGames</url-pattern>
</servlet-mapping>
</web-app>
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:
http://{your-project-name}.appspot.com/getVideoGames?type={type}
And so for me, it looks like:
Feel free to hit the following URLs yourself to get a better idea of what’s going on:
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




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.
Awesome, thank you