Monday, July 25, 2011

Google Docs API International Character Support

If you haven't seen my NoteSync app yet you should check it out: NoteSync. It's a very useful app in my opinion. It lets you take notes on your Android device and automatically synchronizes them with Google Docs. My users find it extremely handy; however, when I launched it I had some upset international users who found that my app did not preserve the special characters in their language. When they would sync with Google Docs they would get the infamous "?" block character in their browser where a special character should have been. This was a huge problem and was surprisingly difficult for me to solve.

I knew the problem had to do with encoding. Google Docs API uses UTF-8. I also knew that it had to do with my upload process since the characters were preserved perfectly downstream. I simply tried encoding the content String like so:

private String utf8Encode(String t) {
try {
byte[] b = t.getBytes("UTF8");
return new String(b, "UTF8");
} catch (UnsupportedEncodingException e) {
return t;

But that had no effect. I took the problem to a couple friends of mine, a Googler, and a professor and mentor of mine from school and I learned that while my character encoding method should work, I was concatenating the UTF-8 string with other regular Java Strings which are UTF-16 by default. After much toiling I finally found the fix and it was surprisingly simple. To wrap my request I'm using the org.apache.http.entity.StringEntity which is then given to my HttpPut object and eventually executed. I discovered that StringEntity has a second constructor with a "charset" parameter. The fix was as easy as putting "UTF8" as the second parameter.

StringEntity docEntity = new StringEntity(data, "UTF8");

That's it. One liner fixes are OK as long as you don't spend days searching for the answer. Hopefully this is of help to someone else with the same issue.

Monday, May 23, 2011

Becoming a Debugging Ninja

I've invested some time recently into learning the Eclipse debugging tools in greater detail. I wrote a blog post for Rain last week detailing some built-in debugging tools that are available that make debugging in Eclipse much more productive. Go check it out and let me know what you think. Please feel free to post any additional tips that you have.

Thursday, May 19, 2011

Hello ADK!

For those of you that either attended or followed the Google I/O 2011 developer conference you probably made a big mental note about the Accessory Development Kit or ADK. This was a HUGE announcement from Google which gave us all a vision of where Android is going -- Everywhere! Going forward, new Android devices sporting the latest builds of Gingerbread, Honeycomb and beyond have the ability to interface with usb peripherals. These external devices are not just limited to just keyboards and mice either; Google has provided developers with an Accessory Development Kit so that we can create unique and innovative accessories that can control or be controlled by Android devices.

Google I/O is famous for swag and this year was no exception. However, Google changed things a bit this year by giving certain devices to just those who attended the related talks. I was lucky enough to be in the right place at the right time to receive an ADK demo board. It's been a week since Google I/O and I decided it was high time to break the ADK out and take it for a spin. This was particularly exciting to me because I don't know hardly anything about hardware. Programming an Arduino based device has been a goal of mine for a while and I'm very excited to get into this.

The whole getting started process was extremely simple. Google has an outstanding article on their Android developer site that guides you through the whole process. The instructions were right on the mark and everything just worked right out of the box. I've made a video documenting tonight's adventure.

Just to give you an idea of the type of scale this technology is capable of, here is a video of me trying to pilot a  bowling ball through a 40 ft. labyrinth game using nothing but a Motorola Xoom connected using the ADK. The video is courtesy of my friend Trevor Fitzgerald who was good enough to come and take some video of me. Thanks Trevor.

I'm going to keep going with the ADK. I think there are some really neat possibilities here.

Saturday, April 9, 2011

Opting out of onCreate()

Today in my Android development I had a need for one of my activities to perform a task only when it is first created. Activities in Android follow a defined life cycle which I pasted below from the site under the documentation for the Activity class. 

The difficulty is that there isn't always a life cycle method that is only called only when an activity is first created. During configuration changes Android will destroy your activity and create it again. Most of the time this is necessary because you may have different layouts for different orientations or actions and so the only safe thing to do is to destroy and create your activity again. There is a way to circumvent this and it is detailed in a section of the Activity class documentation but I think it is worth pointing out here.

You must do two things:

  1. Edit you manifest file under your activity's node and give it: android:configChanges="orientation|keyboardHidden". This will tell Android that you are going to handle these events manually.
  2. Override the method onConfigurationChanged(Configuration newConfig) in your activity. 
Now that you have done that onCreate() will only be called once when your activity first loads. Just be aware of the side effect. Any special layout that you have for your Activity will not be loaded when your orientation changes.