August 16, 2011

QC DBSync version 1.3 available

Posted in Uncategorized tagged , , , , , , , , , , at 5:24 pm by tetontech

For those of you needing a native database synchronization tool version 1.3 of QC DBSync is now available.  It includes minor functionality and API upgrades on the iOS side, an example service written in PHP is included in the download.

QC Native 1.3 available

Posted in Uncategorized tagged , , , , , , , , , , at 3:30 am by tetontech

For those of you doing native, not hybrid JavaScript, development with QC I have uploaded a new version of QC Native.  It rationalizes the differences between the Java and iOS versions making the APIs nearly identical.  I have also updated the API Docs for Android, created API Docs for iOS, and included both in the downloads as well as the QC Family web site.

The download now includes a SimpleDB example for both Android and iOS.  The example inserts values into the database, queries values from the database, and can do an HTTP GET.  The iOS database interactions show how to use CoreData.  I will soon (by the end of the month??) have an example using the Android ORM I’m developing.

Still working on that QC Hybrid release.  It is getting really close.  More on that later.

August 11, 2011

Android Developer Conference

Posted in Android Development tagged , , , , at 6:56 pm by tetontech

Get a discount for AdDevCon II  coming up on November 6 – 11.  You can use my last name (Barney) as a discount code and get $200 off the registration price.  I’d love to meet you there.

 

http://www.andevcon.com/AndevCon_II/index.html

 

Lee

 

February 17, 2011

Android SQLite Library Example Code

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

I have just uploaded QCDBAccess.  It is a SQLite wrapper for Android that is easy to use, thread-safe, and allows you to use transactions if you choose.  It is the same code that has been available for QCAndroid hybrid application developers for a few years.  I thought I would pull it out and give it its own life for those that want to use it on their own.

You can download it from SourceForge and find out more about it, including the API, at the QuickConnectFamily site.

As with all of the things I’m making available I have tried to make this as easy to use, highly functional, and small as possible.

The jar file is only 8k in size but the library doesn’t restrict your use of the databases in any way.

Here is a simple example of how to use the library to do a query against a SQLite database file included in your applications’ assets directory.  Notice that I’m checking to see if the user has entered a name to query against in an EditText object from the user interface.  If they have then I’m going to use a prepared statement.  The EditText object in this example has been passed into the method in which this code exists.

String sql = “SELECT * FROM user”;

String[] statementParams = null;

EditText nameInput = (EditText)methodParameters.get(1);

String name = nameInput.getEditableText().toString().trim();

if(name.length() > 0){

sql += ” WHERE name = ?”;

statementParams = new String[1];

statementParams[0] = name;

}

try {

retVal = DataAccessObject.getData(theActivity, “demo.sqlite”, sql, statementParams);

} catch (DataAccessException e) {

e.printStackTrace();

}

The DataAccessResult variable retVal is going to be returned from this method later.  This DataAccessResult is a Bean that contains resultant field names, the records returned from the query, and a database error description if and only if a database error happened.

the getData method is passed the Activity with which the database is associated with.  This is usually the main Activity in an Android Application.  The second parameter is the name of the SQLite database file to be used.  The third parameter is SQLite SQL assembled either as a standard or prepared statement.  The last parameter is an array of Objects that are to be bound to the ? characters if the SQL is a prepared statement or null if it is not a prepared statement.

Inserting data is done in very much the same fashion.  Once again I have an EditText object in the UI from which I’m getting the name to be inserted into the table.  I’m also generating a unique identifier just to make this a little more interesting.  You could do an auto incremented id in your table if you want.

EditText nameInput = (EditText)parameters.get(1);

String nameToAdd = nameInput.getEditableText().toString().trim();

String[]statementParams = {UUID.randomUUID().toString(), nameToAdd};

DataAccessResult aResult = null;

try {

aResult = DataAccessObject.setData(theActivity, “demo.sqlite”,

“INSERT INTO user VALUES(?,?)”,

statementParams);

return aResult;

} catch (Exception e) {

e.printStackTrace();

}

Notice that once again I have chosen to use a prepared statement.  I’m doing this to avoid SQL insertion attacks.  It never hurts to avoid those.

If you are going to do several insertions you should use transactions to make sure you data stays clean.  There is a startTransaction method and an endTransaction method in the DataAccessObject.  Use those before and after multiple setData calls and you will be safe.

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);
aProtector.protectJSONStream(jsonTestOut1);

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

JSONOutputStream jsonTestOut2 = new JSONOutputStream(testOut);
aProtector.protectJSONStream(jsonTestOut2);

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

JSONOutputStream jsonTestOut3 = new JSONOutputStream(testOut);
aProtector.protectJSONStream(jsonTestOut3);

Thread testThread3 = new Thread(new ProtectorTestWriteRunnable(jsonTestOut1, anObject));
testThread1.start();
testThread2.start();
testThread3.start();

 

} catch (Exception e) {
e.printStackTrace();
}

 

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){

aJsonStream.setProtector(this);

}
protected void claim() throws InterruptedException{
semaphore.acquire(1);
}
protected void free(){
semaphore.release(1);
}
}

 

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());

try{

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

JSONOutputStream aJSONFileStream = new JSONOutputStream(aFileOutStream);

aJSONFileStream.writeObject(aMap);

}

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.

Thanks,

 

Lee

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 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.

December 30, 2010

Pre-Threaded Android and iOS Apps now in QC 1.6.5

Posted in Android Development, iPhone development tagged , , , , , , , at 6:56 am by tetontech

All User Interface libraries, regardless of platform, require that the UI components start their updates from the ‘main’ thread.  Sometimes this is referred to as the UI thread but it is actually the main thread of the application.

What most people that are creating mobile apps don’t realize is that if they are using the main thread to do significant computation such as remote server access, XML parsing, database access, number crunching, etc. is that this makes their User Interface slow and chunky.  The ywill notice the unresponsiveness of their UI but will not know why it isn’t smooth.  They generally tend to come to believe that the problem is that the processors are not fast enough to do what they want to do.  This is generally not the case.

What they really need to do is move the computational portions of their apps into background (worker) threads and let the UI thread remain responsive to the user.  This can be a challenge that they are unwilling to attempt due to the perceived difficulty of implementing threading.

Starting with QuickConnect 1.6.5 I have made the native iOS and Android application templates pre-threaded for both iOS and Android apps.  This means that you, as a developer, no longer need to worry about creating the background threads and then executing UI updates on the main thread for your mobile apps.

When you use one of the mobile native templates in QC all of your validation and computation code is automatically executed on a background thread.  When this portion of your application is complete and you make a UI update the framework automatically causes your update to use the main thread.  You won’t even be aware of the change.

All of this is triggered every time you use the QC framework handleRequest method.  This is in addition to linearizing your asynchronous HTTP remote access calls and an easy to use SQLite wrapper.  With this kind of a head start it becomes much easier for you to create highly responsive, high quality apps in a much shorter time.

I will soon be making this multi-threading available to all of you using the hybrid iOS and Android templates as well.  It should make a huge difference in your execution speeds.

Next page

Follow

Get every new post delivered to your Inbox.

Join 323 other followers

%d bloggers like this: