Short source code examples

This is the source code for a simple Android Preferences demo from the book Beginning Android 3:

Here's the source code for a Scala function to determine whether signs (positive, negative) of two values are opposite:

Without any real introduction or discussion, here's the source code for a Scala class that I use to retrieve Twitter REST content, using the Apache HttpClient library, and the Lift JSON library:

Here's a simple way to get content from a REST web service using Scala:

object GetUrlContent extends App {

  val url = "http://api.hostip.info/get_json.php?ip=12.215.42.19"
  val result = scala.io.Source.fromURL(url).mkString
  println(result)

}

That's a simple, "new" way I do it with Scala. However, note that it handles timeouts very poorly, such as if the web service you're calling is down or running slowly.

FWIW, here's an old approach I used to retrieve REST content (content from a REST URL):

I'm sorry, I don't remember where I found this code, but here's a Scala function to split a camel case string into its components strings (substrings):

An Android HttpClient web services client example. Also shows HttpGet, DefaultHttpClient, DocumentBuilder, DocumentBuilderFactory, ResponseHandler, Document, Element, Node, NodeList, InputSource, GPS, LocationManager, and more.

This is an Android Handler and ProgressBar example, from the excellent book, Beginning Android 3. (See the URL that I have linked to.)

How to create an Android BroadcastReceiver object:

  private BroadcastReceiver receiver = new BroadcastReceiver()
  {
    public void onReceive(Context context, Intent intent)
    {
      runOnUiThread(new Runnable()
      {
        public void run()
        {
          updateForecast();
        }
      });
    }
  };

An Android AlertDialog Builder example:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exception!").setMessage(t.toString()).setPositiveButton("OK", null).show();

How to create a Toast message in Android:

Toast.makeText(this, "No forecast available", 2500).show();

A PHP function that reduces multiple consecutive spaces in a string to one single space, so something like "   " is reduced to " ".

function reduceMultipleSpacesToSingleSpace($text) {
  return preg_replace('/\s+/', " ", $text );
}

A PHP function that strips a string of all characters other than alphanumeric characters:

function onlyAlphanumericAndSpaces($text) {
  # allow only alphanumeric
  return ereg_replace("[^A-Za-z0-9 ]", "", $text );
}
/**
 * Implementation of hook_form_alter().
 * Use these functions to alter the "charity" forms.
 */
function user_validator_form_charity_node_form_alter(&$form, &$form_state) {

  $form['bookverify'] = array(
      '#type' => 'fieldset',
      '#title' => t('Confirmation'),
      '#weight' => 20
    );
  $form['bookverify']['words'] = array(
      '#type' => 'textfield',
      '#title' => t('First five words from Lesson 12'),
      '#descripti