I’ve been using the App Inventor for Android for a while now. I managed to get in on the private beta, partially on my credentials as an MIT Scratch educator. I’ve taught 3 Scratch classes to kids from elementary to high school. I also follow the App Inventor Google group and came across a few people needing a URL Encoder.

I’d already written a simple URL encoder while testing out a mapping application. It uses the Google Maps staticmap API function to retrieve a map with markers. The markers are created by passing in an address, and although many browsers will accept spaces in URLs, the Image component in App Inventor won’t: you must encode a space either with a plus + character or with the hexadecimal encoding %20.

The newline character “\n” is also encoded, but the routine to test for it is separate from the others as App Inventor doesn’t find the correct location of various characters when you include it.

Here are the blocks for the character codes and their translations, plus some misc variables used in the encode routine. The “chars” block contains $&+,/:;=?@ “<>#%{}|\^~[]’ and the codes block contains 24262B2C2F3A3B3D3F4020223C3E23257B7D7C5C5E7E5B5D60.

Now the URLEncode routine itself is fairly simple. It loops through each character in the provided text and searches for it in the chars and ctrl strings. If it finds it, it calculates the offset position for the code and adds that to the URL. If it doesn’t find it, then it just adds the character from the provided text. The offset position for each character is ((pos-1)*2)+1.

3 Responses to “URLEncode in the App Inventor for Android”

Leave a Reply for buy2let