January 25, 2011

QCJSON Now Thread Safe

Posted in Android Development, misc tagged , , , , , , , , , , , , at 7:25 am by tetontech

The changes described here are found in QCJSON v. 1.0.2 available on sourceForge .  The license is BSD.  You can get the source code from the git repository on sourceForge using the command:

git clone git://qcjson.git.sourceforge.net/gitroot/qcjson/qcjson

or look at it online.

I’ve been pondering how to make QCJSON thread safe meaning that you could read and write to the same stream from separate threads safely without impacting any other streams that may be being used in the same or other threads.

The problem description is that you don’t want to start a read when one is part way complete.  Nor do you want to start a write in the middle of another write.  Both of these situations plus mixing read and write can cause serious data corruption.

I briefly played with the idea of creating a Class that would contain a static semaphore.  This static semaphore would control access to the reading and writing behavior of the existing code.  The problem is that with this approach reading or writing to one stream on one thread would cause any other threads to wait at the semaphore claim location even if the stream that it wished to write to was in no way associated with the stream that was currently being used.  In other words reading or writing on one stream would block reading and writing on all streams until it was complete.  Obviously this is an unacceptable solution.  You might as well just write in one thread.

Since there is no way of interrogating an input our output stream object in Java it isn’t possible to hide the threading locks from the programmer using the QCJSON library and allow independent streams to work in independent threads.

While I’ve thought through several options the one that seems the best is to allow you, the developer, to pair input and output streams and include a semaphore in this pairing.  This is going to require a new class.

For lack of a better name I am calling this new class org.quickconnect.json.JSONStreamProtector.  It has a default constructor that takes no parameters and has one public method, protectJSONStream(JSONStream aStream).

If you instantiate a JSONStreamProtector you can pass the protectJSONStream method any number of JSONInputStreams and JSONOutputStreams.  Any streams that you pass as a parameter to this method will share the same semaphore.  Thus when you call readObject or writeObject on the protected streams any threads that would be using these streams will block in their readObject or writeObject methods until it is safe to proceed.

Using JSONStreamProtector

Changes have been made in the JSONInputStream and JSONOutputStream classes so that  if a protector has been set for them the stream will claim or wait until it is able to claim the semaphore in its defined protector.

You needn’t put the protector in a collection or some other class’ attributes in order for it to not be garbage collected.  Passing one JSONStream to the protectJSONStream method is sufficient.

In Example 1 the code shows how to protect a single output stream from three separate threads trying to write to the stream simultaneously.  Input streams work the same way.  Protecting an input/output stream pair that work on the same underlying resource would just mean using the same protector for both streams.

Any stream can only have one protector at a time.  This keeps your code from accidentally getting in a cross-locking situation.  You can switch the protector is is using at any time by calling the new protectors’ protectJSONStream method.

Example 1:

try {
* Add three output streams to the same file and then have each of them send
* json to the stream. Make sure that no data corruption occurs.
JSONStreamProtector aProtector = new JSONStreamProtector();
FileOutputStream testOut = new FileOutputStream(aFile);
* stuff to write to file
int[] testIntArray = {7,10,0,-789};
String[] testStringArray = {"What","is","happening?"};
TestObject anObject = new TestObject("Hello there.", 7, new Date(1067899));


* do the running
JSONOutputStream jsonTestOut1 = new JSONOutputStream(testOut);

Thread testThread1 = new Thread(new ProtectorTestWriteRunnable(jsonTestOut1, testIntArray));

JSONOutputStream jsonTestOut2 = new JSONOutputStream(testOut);

Thread testThread2 = new Thread(new ProtectorTestWriteRunnable(jsonTestOut1, testStringArray));

JSONOutputStream jsonTestOut3 = new JSONOutputStream(testOut);

Thread testThread3 = new Thread(new ProtectorTestWriteRunnable(jsonTestOut1, anObject));


} catch (Exception e) {


The source code for the protector class is simple so I’ll just put it here.

package org.quickconnect.json;

import java.util.concurrent.Semaphore;

public class JSONStreamProtector{
private Semaphore semaphore = new Semaphore(1, true);
public void protectJSONStream(JSONStream aJsonStream){


protected void claim() throws InterruptedException{
protected void free(){



January 21, 2011

Android and JSON

Posted in Android Development, misc tagged , , , , , , , , , at 7:12 pm by tetontech

As I have been working with Android I found that the JSON library included in the standard Android library set was the one from JSON.org which is intended as a reference implementation.  When using that implementation I found that it is much more confusing than it needs to be.  To solve this problem I created a generic Java implementation usable in any Java situation including Android.  You can get it from sourceForge.  A JavaDoc API is included in the download.

Like the JavaScript implementation from JSON.org that is being embedded in all new browsers my implementation has stringify and parse methods.  These methods behave just like the ones in JavaScript and are static methods of the JSONUtilities class.

The stringify method  receives most any Serializable object, runs up its inheritance tree, and returns a JSON string that includes all of the attributes of all of the objects and all of the values from all of the collections found.  The only Serializable objects not accepted are raw Java Objects, since they have no attributes, and anything that inherits from java.awt.container.

The JSONUtilities.parse method takes a JSON formatted string as its parameter and returns either a HashMap or an ArrayList.  What is returned is dependent upon if the JSON container is an array or an associative array(object/map).

Here is an example from the JavaDocs of the string being parsed and what is produced.

  • Example 1 JSON: [“1”, “hello”, {“name”:”fred”,”age”:”23″}]
  • Example 1 Result: An ArrayList with three values: a String “1”, a String “hello”, and a HashMap as the third value in the ArrayList. This HashMap has two key/value pairs: “name”/”fred” and “age”/”23”.


  • Example 2 JSON: {“state”:”Idaho”, “city”:”Rexburg”, “people”:[“bob”,”sue”]}
  • Example 2 Result: A HashMap with three key/value pairs: “state”/”Idaho”, “city”/”Rexburg”, and “people”/ArrayList. The ArrayList that is the value for the “people” key has two String values “bob” and “sue”.

Object anObject = JSONUtilities.parse(aJSONString);

Here is an example of how the JSONUtilities stringify method works.

  • Example 1 Object: An ArrayList with three values: a String “1”, a String “hello”, and a HashMap as the third value in the ArrayList. This HashMap has two key/value pairs: “name”/”fred” and “age”/”23”.
  • Example 1 JSON result: [“1”, “hello”, {“name”:”fred”,”age”:”23″}]


  • Example 2 Object: A HashMap with three key/value pairs: “state”/”Idaho”, “city”/”Rexburg”, and “people”/ArrayList. The ArrayList that is the value for the “people” key has two String values “bob” and “sue”.
  • Example 2 JSON result: {“state”:”Idaho”, “city”:”Rexburg”, “people”:[“bob”,”sue”]}

String  jsonString = JSONUtilities.stringify(anObject);


In addition to these two utility methods there are two stream wrapper classes.  These behave much the ObjectOutput stream methods that are part of JavaSE.  They allow you to JSON an object into a stream and parse an object out of a stream.  These streams can be anything that inherits from InputStream or OutputStream.  For example, these could be a FileInputStream, a FileOutputStream, an input or output stream from a socket, etc.

Here is a code sample where the JSONOutputStream is wrapped around a FileOutputStream.  This would write the JSON to a file on disk.

HashMap aMap = new HashMap();

aMap.put(“stringOne”, “Some sort of string”);

aMap.put(“aNumber”, 16.5);

aMap.put(20,”some other stuff” );

aMap.put(“aTesterDate”,new Date());


FileOutputStream aFileOutStream = new FileOutputStream(“testFile.txt”);

JSONOutputStream aJSONFileStream = new JSONOutputStream(aFileOutStream);



catch(Exception e){

//do your exception handling



The code to read and parse JSON from a stream is similar.  Here is an example reading from a file.

try {

FileInputStream aFileInputStream = new FileInputStream(“testFile.txt”);

JSONInputStream inFromFile = new JSONInputStream(aFileInputStream);

Object anObject = inFromFile.readObject();

//if anObject is expected to be an ArrayList cast it as such and use it.

//if anObject is expected to be a HashMap cast it as such and use it.

} catch (Exception e) {

//do your exception handling here



I hope this helps you with your data communications in Android, JavaSE, and Java Enterprise.


January 15, 2011

QuickConnectFamily 1.6.7 Available

Posted in Android Development, iPhone development tagged , , , , , , , , , , , , , , at 11:14 pm by tetontech

QC 1.6.7 has been uploaded.  It includes a complete rewrite of the way your QCAndroid apps are compiled from within xCode.  I think you will find compiling much better.  It also includes a native Android template that allows you to build QC applications in Androids’ native Java along side the Objective-C native template for iOS apps.

Just like in QC Native for iOS apps created using this QC Native for Android template ‘pre-threads’ your application for you.  This means that any code that you write that is not directly related to updating the view is run in helper threads.  Any code you write that updates the view will run in the main view thread just like the native QC iOS apps.

Build your Control stacks in either language and your Validation and Business Control Objects, including db and server/service access, will be run on background, helper threads and then when the View Control Objects are executed they will be run in the main-view thread.

This means that your applications’ user experience is snappier since the main-view thread is only blocked when the view is being updated.

I will be covering the basic structure of Android Native QC apps and how to write them at the Android Developer Conference in March.

I will be covering the basic structure of iOS Native and Hybrid QC apps at the iPhone Developers Conference in April.

More tutorials, when I get them done, will be available on the wiki.

I have also uploaded QuickConnectFamilyPC_1.6.7 for those of you who want to develop using Eclipse rather than Xcode.  It includes a few examples of hybrid apps and instructions for how to use QC in Eclipse.

I’d love to see you at the developer conferences and get feedback in addition to what I receive via the Google group and this blog.




Android Emulator Defect Causes App Crash

Posted in Android Development tagged , , , , , , at 7:00 pm by tetontech

Avoid the current Gingerbread Android SDK if you use the emulator to develop your apps!

The current Android SDK version, Gingerbread- r_08, has a new defect in the emulator.  This defect causes any application that uses the standard Google/Android methodology of communication between JavaScript in the Android WebView and other Java in the application to crash when run in the emulator.

Installing and running your QuickConnect or other hybrid apps on a device still works.  Since developing using a device is faster than developing using the emulator this is probably your bet anyway.

A discussion of this bug can be found in this  Google group discussion.

Let’s hope the Android team either gets their act together and fixes this soon or gives us a better development tool like Android running in a real VM instead of this emulator.

January 6, 2011

QuickConnect and the Mac app store

Posted in misc tagged , , , , , , at 7:50 pm by tetontech

The mac app store is up.  Do you want to put an app up?  QC has supported hybrid mac development for two years.  It is ready for you now.  All you have to do is make your selection as you see in the picture below and start making your app.

I’m currently adding more functionality to QC Mac.  The update should be out next week but you can get stared now.  Develop your app in HTML, CSS, and JavaScript just like your hybrid iOS, Android, Blackberry apps.

Selecting QC for the Mac

QuickConnect 1.6.6 now available

Posted in iPhone development tagged , , , , , , at 3:05 am by tetontech

QC 1.6.6 has a fixed installer but also has an upgrade that should make your user interface more responsive when you use the framework calls to download or upload files, make native database queries, or any other calls you make down the the native layer.  You don’t need to worry about the threading issues or starting the threads.  It is all automatic.  It doesn’t have any impact on your JavaScript code.

January 4, 2011

QuickConnect family Release for Windows, Linux, etc.

Posted in Android Development, erlang development, PHP development tagged , , , , , , , , , at 5:23 pm by tetontech

I have put together a QC 1.6 release for non-Mac users.  It includes a jar file for QCNativeAndroid, files for QCPHP, QCErlang, and the 1.5 release of QCHybridAndroid.  As soon as I can get to it I will be updating the hybrid iOS and hybrid Android environments to include the multi-threading now available in the native iOS and native Android environment.  It will be interesting to see how the available threading effect what we can do in JavaScript.

January 2, 2011

2010 Blog Stats.

Posted in Uncategorized at 6:34 am by tetontech

Thought you might like to know.

Thanks for your support.




The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

Healthy blog!

The Blog-Health-o-Meter™ reads Wow.

Crunchy numbers

Featured image

The Louvre Museum has 8.5 million visitors per year. This blog was viewed about 96,000 times in 2010. If it were an exhibit at The Louvre Museum, it would take 4 days for that many people to see it.

In 2010, there were 34 new posts, growing the total archive of this blog to 139 posts. There were 7 pictures uploaded, taking up a total of 765kb.

The busiest day of the year was July 6th with 439 views. The most popular post that day was UIWebView Example Code.

Where did they come from?

The top referring sites in 2010 were stackoverflow.com, quickconnect.sourceforge.net, quickconnectfamily.org, maniacdev.com, and swik.net.

Some visitors came searching, mostly for uiwebview example, quickconnect, uiwebview sample, android ajax, and uiwebview javascript callback.

Attractions in 2010

These are the posts and pages that got the most views in 2010.


UIWebView Example Code May 2008


Calling Objective-C from JavaScript in an iPhone UIWebView August 2008


QuickConnect iPhone: an iPhone UIWebView hybrid framework May 2008


Android, WebView and AJAX February 2009


iPhone Objective-C SQLite development June 2008

%d bloggers like this: