Short source code examples

This Scalatra POST method shows one way to handle HTTP POST data. It shows that you can access POST parameters using the Scalatra params function:

I created this Scala class as a way to test an HTTP POST request to a web service. Although its written in Scala, it uses the Apache HttpClient Java libraries. I got the NameValuePair code from the URL I've linked to.

If you have a Scalatra POST method like this:

post("/foo") {
  // do stuff here
}

you can access the data that's sent to your POST function by using the implicit request object, like this:

val content = request.body

Then you can operate on that content as desired.

I just generated this example build.sbt SBT file when experimenting with Scalatra. I manually added the MongoDB Casbah dependency line.

First, I went to the URL shown.

Second, I updated SBT to the latest 0.11.x version.

Then I added this line:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.0.0-M3")

to the file ~/.sbt/plugins/build.sbt (Note that there is an error in there docs, with an extra "}".)

I was then able to run this command:

How to find all files that have been added within the last day on a Mac OS X system: This command doesn't seem to be as accurate as the Unix find command, but it seems to be a bit faster:

mdfind 'kMDItemFSCreationDate >= $time.yesterday' | grep -v Cache | grep ".jar"

I just tried to use it to find some files that were recently downloaded by Maven, but it didn't actually work very well. That being said, I hope that a variation of this mdfind command will be helpful.

You can use Java Collections in a Scala app by using this import statement:

import scala.collection.JavaConversions._

I just tested the following Scala application that gets a Java collection from the MongoDB Java driver, and I can confirm that this works:

How to start MongoDB with a different database directory:

bin/mongodb --dbpath /Users/Al/data/mongodatabases

 

 

The Android API makes extensive use of what I thought were "static inner classes", but apparently the correct term is "static nested class". Here's a description of the differences between static and non-static nested inner classes from the URL shown:

"A non-static nested class (or 'inner class') has full access to the members of the class within which it is nested.

Using a parallel map to transform a collection of String to all-uppercase

From time to time I have a problem with Android Studio where it won’t show the logcat output of an app I’m trying to debug. I haven’t been able to figure out why that is, so what I do instead is look at my logcat output from my command line.

As a short tip, if you want to see Android LogCat output from your command line, just run this adb command from your Unix/Linux command line:

This isn't my usual "source code snippet", but I thought this was a good description of UTF-8:

UTF-8 is almost exactly like regular ASCII text, except it has escape codes for non-ASCII characters such as Japanese glyphs.

This comes from the book, Hello Android.

A Scala Akka 2.0 Actor "ping pong" example:

This source code shows a simple example of how to create an Android ListActivity and ListView, using a static array of elements for the list.

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.