NoClassDefFoundError After ADT Update

I’ve just returned to an Android project after a couple of months’ pause. After updating Eclipse and ADT, my project started throwing an exception:

06-12 21:45:26.154: ERROR/AndroidRuntime(3654): java.lang.NoClassDefFoundError: com.google.gson.gson

This JAR file was clearly in my “lib” folder in the project and in my build path. After entirely too much fiddling about, cleaning, rebuilding, and googling, I discovered my problem. Apparently, ANT is now expecting these to be in the “libs” folder. After moving all of my third party JAR files from “lib” to “libs”, deleting “lib”, and restarting Eclipse, all is right the the world again!

Restart Forever.js After Reboot

I’m amid migrating from a Dreamhost shared server to a VPS. A benefit of this is the ability to have long running processes like node.js. I’m also migrating the back end for Menu Line George to Parse.com. My v1 process for recording the George calls was a kludge of scripts running on my home desktop to call via Skype. This was more a proof-of-concept than a long term solution. Now I’ll be using Twilio + node.js + Parse as my permanent solution, significantly reducing how much I’ll need to think about it on a daily basis.

The simplest way I’ve found to keep a node script running as a service is forever.js. If you’re new to node, I suggest checking it out. Otherwise, I’m sure you’ve heard of it.

I fired it up yesterday and it worked like a champ. That is, of course, until my VPS rebooted overnight for some reason and the call wasn’t made this morning. I found a handy little guide over at Hack Sparrow for creating a cron job to restart your node script with forever when the machine reboots.

First create a shell script like this one (I called mine app-starter.sh):

#!/bin/sh

if [ $(ps aux | grep $USER | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
        export NODE_ENV=production
        export PATH=/usr/local/bin:$PATH
        forever start ~/example/app.js > /dev/null
fi

Then make the script executable:

chmod 700 ~/app-starter.sh

Now make cron run the script when the system is rebooted. Add this to your crontab (via crontab –e):

@reboot ~/app-starter.sh >> cron.log 2>&1

For the uninitiated like myself, use CTRL+K,X to save.

Thanks to Hack Sparrow for taking the time to share this.

via Hack Sparrow.

XML Shredding In SQL Is A Recipe For Arthritis

At work I’ve been spending a lot of time the past couple of weeks doing the same thing: serialize the records in a table as XML, pass it up to the client, then back down to another database to be deserialized and merged. It’s nothing fancy, but it’s certainly tedious. It’s generally been a pattern of:

Generate the XML: (easy)

SELECT
  Column1,
  Column2,
  Column3,
  Column4,
  Column5
FROM FooTable Foo
FOR XML AUTO, ROOT('Root')

To create something like this:

<Root>
<Foo Column1="Value1" Column2="Value2" Column3="Value3" Column4="Value4" Column5="Value5" />
</Root>

But having to type this out has been the arthritic step. For larger tables, this can take a while.

SELECT
	a.value('@Column1', 'varchar(20)') Column1,
	a.value('@Column2', 'varchar(20)') Column2,
	a.value('@Column3', 'varchar(20)') Column3,
	a.value('@Column4', 'varchar(20)') Column4,
	a.value('@Column5', 'varchar(20)') Column5
FROM
	@XmlValue.nodes('/Root/Foo') T(a)

I know of nothing to automatically generate something like this, and yesterday I decided enough is enough (after coming to a table that was 65 columns wide). I wanted a way to quickly write the SELECT clause of something like this for me. Quick and easy was important, because if it were a chore to use I’d likely just write it out, grumbling the whole time.

The table definition is easy to generate via Management Studio. Right Click the window > Script Table As > Create To > New Query Window and you get something like:

CREATE TABLE [dbo].[FooTable](
	[Column1] [varchar(20)] NULL,
	[Column2] [varchar(20)] NULL,
	[Column3] [varchar(20)] NULL,
	[Column4] [varchar(20)] NULL,
	[Column5] [varchar(20)] NULL,
//...[constraint and references]...

I settled on using the clipboard as the medium so I don’t need to deal with saving a file or using a form for input. With this pinned to my taskbar, I just copy the table definition text to my clipboard and click the icon. Voila! The result is ready to be pasted. There’s nothing fancy here. Just convenience.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace MasterShredder
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string contents = Clipboard.GetText();
            if (string.IsNullOrEmpty(contents))
            {
                Console.WriteLine("I'm expecting the column definition to be in your clipboard.");
                Console.WriteLine("Expected format:  [ColumnName] [ColumnType] [NULL or NOT NULL],");
                Console.Read();
            }
            else
            {

                string[] lines = contents.Split('\n');
                StringBuilder result = new StringBuilder();

                foreach (string line in lines)
                {
                	//Strip new line and tab characters.
                    string cleanLine = line.Replace(Environment.NewLine, "").Replace("\t", "");
                    string[] parts = cleanLine.Split(' ');
                    if (parts.Length > 2)
                    {
                        string name = parts[0].Replace("[", "").Replace("]", "");
                        string type = parts[1].Replace("[", "").Replace("]", "");

                        // type definition was split. i.e. decimal(18, 6)
                        if(parts[2].EndsWith(")"))
                        {
                            type += parts[2];
                        }
                        string lineResult = String.Format(",a.value('@{0}', '{1}') {0}", name, type);
                        result.AppendLine(lineResult);
                        Console.WriteLine(lineResult);
                    }
                }

                if(!string.IsNullOrEmpty(result.ToString()))
                {
                    Clipboard.SetText(result.ToString());
                }
            }
        }
    }
}

Oh, and if this is crappy, fork and fix it on gist.

Using simple_list_item_2 with an ArrayAdapter (Android)

Today I wanted to display static a ListView of properties and their values. It’s Friday, so I really wanted to do this with as little work as possible. Android’s free layout simple_list_item_2 sounded like what I wanted, but it wasn’t immediately clear how to handle this. There are a lot of lazy ways we can list single values, but Googling around how to use simple_list_item_2 resulted in lots of questions with no real answer.

I found Mark Assad, who parsed the system layout files into raw XML. The led to the magic answer of TwoLineListItem widget. Finding this widget allowed me to write a simple ArrayAdapter of key/value pairs.

adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_2,list){
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            TwoLineListItem row;
            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);
            }else{
                row = (TwoLineListItem)convertView;
            }
            BasicNameValuePair data = list.get(position);
            row.getText1().setText(data.getName());
            row.getText2().setText(data.getValue());

            return row;
        }
    };
listView.setAdapter(adapter);

Hope this saves someone else some time, as I spent an embarrassing amount of time determined to use the simple_list_item_2 layout.