Rob Heittman — July 12, 2013
In Part 1 we demonstrated the basics of communication with Appygram. But one of the most important features of Appygram is its ability to route messages to the right people based on different topics. If you have different teams or vendors collaborating on an app, this can help connect the right responders to the task.
In the Appygram web interface, you can define multiple topics, each of which has its own message forwarding rules. Instead of hard-coding that list of topics in your app, you can fetch it dynamically using the Appygram API.
The topic list comes back from Appygram in JSON format, and looks like this:
[
{id: 1, name: 'Topic One'},
{id: 2, name: 'Topic Two'}
]
The IDs aren't especially important to us right now; we just need the names so we can pass them along in the topic field of our Appygram message.
This gives us a good excuse to explore nice ways of handling JSON in Android and Java. Since API 11, Android has its own JSON implementation, e.g. http://developer.android.com/reference/android/util/JsonReader.html and there are lots of recipes for building your own.
In my example, I'm going to use the Gson library, which along with Jackson is one of the more commonly-seen third party JSON libraries. The main advantage of the larger libraries, in tradeoff for their size and some performance penalties, is that they have built-in intelligence to do type-safe serialization and deserialization to Java classes. That translates to less parsing code and primitive-handling in your application code. I think it makes for more readable examples, so our Android series will go forward with this approach.
Also, if you've got any nontrivial app that uses a lot of third party libraries, have a snoop around: many commercial libraries that speak to JSON APIs introduce a Gson dependency already (possibly repackaging it inside their own JAR) so you might already be bundling Gson into your APK anyway.
The application complexity savings are fairly instant - once I add Gson to the libs of our sample project, I can stop messing around with constructing my own HTTP parameters. I get to replace all this:
private String join(List<String> aArr, String sSep) {
StringBuilder sbStr = new StringBuilder();
for (int i = 0, il = aArr.size(); i < il; i++) {
if (i > 0)
sbStr.append(sSep);
sbStr.append(aArr.get(i));
}
return sbStr.toString();
}
// ...
List<String> httpParams = new ArrayList<String>();
for(Map.Entry<String,String> p : params.entrySet()){
try {
httpParams.add(p.getKey() + "=" +
URLEncoder.encode(p.getValue(),"UTF-8"));
} catch (UnsupportedEncodingException impossible) {
throw new Error(impossible); // UTF-8 should be present
}
}
String input = join(httpParams,"&");
with this:
String input = new Gson().toJson(params);
I do need to add a Topic class with some getters and setters to serve as a model object. Then I can use Gson to retrieve a List of my Topics in a reasonably terse and type-safe fashion.
URL url = new URL("https://arecibo.appygram.com/topics/" +
APPYGRAM_API_KEY);
HttpURLConnection con =
(HttpURLConnection) url.openConnection();
JsonReader reader = new JsonReader(
new InputStreamReader(con.getInputStream(), "UTF-8"));
reader.beginArray();
while (reader.hasNext()) {
Topic topic = gson.fromJson(reader, Topic.class);
topics.add(topic);
}
reader.endArray();
reader.close();
In the example app, we use this info to draw the topic selection into a spinner, then harvest the value and send it along with the Appygram message.
If you fill in your API key, you've now got a working example of all the Appygram basics and a nice mechanism for working with the Appygram APIs.
Next in this series, we'll start filling up that message with information that the responders can use to save time and energy. Using our mighty and terse new JSON mappings, we'll pass some useful information in the app_json
datablock and pull it out on the web service side.
Related tag(s): Mobile Apps How to Breaking
Appygram is a web and mobile application communication service.
Plans start at FREE! Sign Up Now!