Widely regarded as the successor to XML in the browser, JSON (JavaScript Object Notation) aspires to be nothing more than a simple and elegant data format for the exchange of information.
JSON's basic types are:
* Number (integer, real or floating point)
* String (double-quoted Unicode with backslash escaping)
* Boolean (true and false)
* Array (an ordered sequence of values, comma-separated and enclosed in square brackets)
* Object (collection of key: value pairs, comma-separated and enclosed in curly braces)
* null
This article gives an insight about how to use JSON as a data interchange format, provided with the URL of a Restful Service which returns a JSON response.
Advantages of JSON
* Simpler than XML because it is not a markup language and a natural representation of data
* JSON is better data exchange format; XML is a better document exchange format
* JSON is easier to read for machines with no/thin client-side library
* JSON is a natural fit for data consumption by browser clients, for example Ajax components
* Ability to represent general data structures: records, lists and trees
* Parsing and generating JSON support in 21 languages
Disadvantages of JSON
*JSON does not have a <[CDATA[]]> feature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads.
* Unlike XML, JSON does not provide any display capabilities because it is not a document markup language. JSON was not even intended for that purpose.
* JSON is not extensible - it does not need to be because it's not a document markup language.
JSON is a newer format so not many tools as yet to help with authoring & parsing , however, some available ones are:*JSON Tools - Java Tools for the JSON Format (parser, renderer, serializer, mapper, validator)*JSON-lib - Java library for transforming beans, maps, collections, java arrays and XML to JSON and back again to beans.*JSON in Java - Java APIs from json.org (see more below)*JSON-taglib - JSON-taglib is a JSP 2.0 tag library used to render JSON data from within JSP code.
Comparison of JSON with XML
Simplicity
XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.
Extensibility
JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.
Interoperability
JSON has the same interoperability potential as XML.
Openness
JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.
Attached is a small sample as to how to use JSON as a data interchange format in Android.
The activity hits a JSON URL and gets the response as a stream. The response is consumed as HTTPEntity (provided by Apache and comes with the Android SDK) and the input stream is the extracted from the entity.
And the response in the form of HTTPEntity is converted into a String. This string is then cast into a JSONObject.
String result = convertStreamToString(instream);
Log.i("REST: result", result);
JSONObject json = new JSONObject(result);
This response helps us to manipulate it in a dynamic manner. The response JSONObject contains a key/value pair which is consumed and printed in the log as JSONArray.
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
This sample would need you to see the results in the log as to how a JSON response can be obtained as a name value pair and then bind to the UI as a dynamic text/image etc.
Friday, February 20, 2009
Wednesday, February 18, 2009
View Stubs in Android
This article is aimed at understanding the ViewStub feature in the Android SDK. ViewStub is a feature which allows the run time inflation of views. This is especially useful in creating tree nodes and inflating sub-tree nodes dynamically.
The idea is to inflate a view only when required and thereby minimize resource utilization. When the inflate() method is called on the ViewStub, it replaces itself with its parent.
There are two important methods in the ViewStub API:
android:inflateId - This is used to override the id of the inflated view
android:layout - The layout resource that should be passed to identify the resource that needs to be inflated
Here’s sample example that demonstrates how to use this feature effectively:
Create a XML resource file like below:
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/node1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Node1" />
<ViewStub android:inflatedId="@+id/stub1" android:id="@+id/id_stub1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout="@layout/ layout_1" android:layout_height="wrap_content" />
<TextView android:id="@+id/node2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Node2" />
<ViewStub android:inflatedId="@+id/stub2" android:id="@+id/id_stub2" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout="@layout/layout_2" android:layout_height="wrap_content"/></LinearLayout>
Below is the code used to demonstrate a tree view which has two main nodes. In the example stated, the main tree nodes are in the form of Text Views. Below onCreate method of the activity creates listeners for both the Text Views :
node1 = (TextView) findViewById(R.id.node1);
node1.setOnClickListener(this);
node2 = (TextView) findViewById(R.id.node2);
node2.setOnClickListener(this);
Implementation of the onClick listener to inflate the child nodes on the click of parent nodes is below
@Override
public void onClick(View arg0) {
if (arg0 == node1) {
if (subTree1View == null) {
subTreeStub1 = (ViewStub) findViewById(R.id.id_stub1);
subTree1View = subTreeStub1.inflate();
}else{
toggle(subTree1View);
}
} else if (arg0 == node2) {
if (subTreeStub2 == null) {
subTreeStub2 = (ViewStub) findViewById(R.id.id_stub2);
subTree2View = subTreeStub2.inflate();
}else{
toggle(subTree2View);
}
}
}
private void toggle(View view) {
if(view.getVisibility()== View.VISIBLE)
view.setVisibility(View.GONE);
else if(view.getVisibility()== View.GONE)
view.setVisibility(View.VISIBLE);
}
A noticeable point here is that a reference of the view is given to the application without a findViewById on inflation.
The ViewStub can also be used on rule based screens, i.e., when the screen layout changes depending on user input or other rules. The View.OnInflateListner is another call-back which can be used after inflating the views.
The idea is to inflate a view only when required and thereby minimize resource utilization. When the inflate() method is called on the ViewStub, it replaces itself with its parent.
There are two important methods in the ViewStub API:
android:inflateId - This is used to override the id of the inflated view
android:layout - The layout resource that should be passed to identify the resource that needs to be inflated
Here’s sample example that demonstrates how to use this feature effectively:
Create a XML resource file like below:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/node1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Node1" />
<ViewStub android:inflatedId="@+id/stub1" android:id="@+id/id_stub1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout="@layout/ layout_1" android:layout_height="wrap_content" />
<TextView android:id="@+id/node2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Node2" />
<ViewStub android:inflatedId="@+id/stub2" android:id="@+id/id_stub2" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout="@layout/layout_2" android:layout_height="wrap_content"/></LinearLayout>
Below is the code used to demonstrate a tree view which has two main nodes. In the example stated, the main tree nodes are in the form of Text Views. Below onCreate method of the activity creates listeners for both the Text Views :
node1 = (TextView) findViewById(R.id.node1);
node1.setOnClickListener(this);
node2 = (TextView) findViewById(R.id.node2);
node2.setOnClickListener(this);
Implementation of the onClick listener to inflate the child nodes on the click of parent nodes is below
@Override
public void onClick(View arg0) {
if (arg0 == node1) {
if (subTree1View == null) {
subTreeStub1 = (ViewStub) findViewById(R.id.id_stub1);
subTree1View = subTreeStub1.inflate();
}else{
toggle(subTree1View);
}
} else if (arg0 == node2) {
if (subTreeStub2 == null) {
subTreeStub2 = (ViewStub) findViewById(R.id.id_stub2);
subTree2View = subTreeStub2.inflate();
}else{
toggle(subTree2View);
}
}
}
private void toggle(View view) {
if(view.getVisibility()== View.VISIBLE)
view.setVisibility(View.GONE);
else if(view.getVisibility()== View.GONE)
view.setVisibility(View.VISIBLE);
}
A noticeable point here is that a reference of the view is given to the application without a findViewById on inflation.
The ViewStub can also be used on rule based screens, i.e., when the screen layout changes depending on user input or other rules. The View.OnInflateListner is another call-back which can be used after inflating the views.
Monday, February 16, 2009
Building your first Iphone Application - PART I
Sooner or later most of the mobile application developers will have to do the switch to one or multiple mobile platforms as more and more App stores are sprouting up and large innovators like Google join the mobile revolution. Iphone is relatively new but a very mature platform which paved the way for the new mobile generation.
There may be many reasons why someone would want to be an app developer for the Iphone. Mobile platform, technology, money & fame, Innovative, etc. Even though the upfront cost is more and requires non-apple platform users/developers to do the switch, Apple created the App store in such a way that a good application can actually mean a lot of ROI (10s or even 100s of times more than what is spent for developing the same) and fame to the platform as well as the developer who built it. It is also the sure path to become innovative.
With this article, I am starting a tri-part series on how to setup the OS X environment and build your First Iphone application. Feel free to skip this part if you already know how to get the XCode interface up and running on OS X Leopard.
Who can do it
Anyone from any platform Windows, Unix, etc can do it. The platform switch sounds difficult & scary but not anymore after you see how easy it is to start OFF, it get to be a lot of fun.
Cost
The upfront cost to build an Iphone application, if you don't already own a Mac computer is around $900 while discounting the time spent learning the Objective C language constructs and writing the application code using XCode. Though Apple did an Amazing job to reduce the complexity of development by structuring the entire development & deployment process, it is still difficult for non-apple platform developers to get on the band wagon.
For those who are beginners -
The easiest way to start is by getting hold of a Mac (IMac, MacBook or any Mac hardware based computer) with OS X Leopard to setup the development environment for the application that we're going to build. I tried finding ways to setup a virtual machine, and most of the blogs out there say that it is not possible for 3 reasons:
1) Apple does not allow this in their OS licensing
2) Mac OS X specifically checks to ensure that it is installing on Apple hardware
3) Mac OS X requires that the computer has an APIC - which we do not emulate
Sometime last year Apple decided to open up their software platform to let the users/developers benefit from virtualizing OS X. But the catch is it can be done only on MAC hardware.
So, let's do the costing math
Mac Mini - $599.00
Mac OS X - $129.00
Apple Developer License - $99.00 (you need this at least at the end to publish an application in the app store)
You can always look for a Refurb Mini which can reduce the cost by another $100-$200. But even then the overall upfront cost to start is around $500 assuming you have a keyboard/mouse & a monitor for reuse from the computer you have at home.
I tried looking into this further to find out if there is any other way to do this cheap. i found one other way where you only need a licensed copy of OS X Leopard.
Note: Don't try this unless you're OK wasting time & the $129 if you realize that this may not support the development environment. The PEARPC Mac hardware emulator. Install instructions, refer to pearpc from sourceforge or http://pearpc.sourceforge.net/. This option would still cost you $129 assuming you have a windows based personal computer at home. (You're on your own if you're doing this.)
The DEV environment
Download & Install the free Iphone SDK by registering at Apple iphone developer portal. For now, you don't need to opt for the $99 DEV license as building application doesn't require it. To download, you'll need to register with apple and that is FREE.
However, The free download option will not let you download the applications you build on to your Iphone device for end to end performance testing. It'll also not let you submit the final application to be posted in the App store. But you'll not need this to learn and be comfortable with the platform switch. If you're so comfortable, there is always the Jailbroken mode to get your app into the Device the hard way...
Iphone SDK Contents
XCode - Apple's integrated development environment (IDE). This includes editor for creating, debugging source code, compilation and performance tuning tools.
Simulator - This will allow the developers to run their Iphone programs on Mac.
All set and before you begin
All the code you write must be in Objective C. This is very similar to C#, Java or any other objective programming language. I've heard/learnt somewhere that even JS can also be used to create an Iphone application. If you're not already familiar with Objective C, you may refer to the reference guide available thru the apple developer connection, http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/objectiveC.
The xcode Iphone project templates...
Also, make yourself familiar with the Iphone/Ipod interface because you wanted to build something for this platform.
Limitations
1) The platform currently allows only 1 actively running application other than the OS at any given time. (It is phone running on battery!)
2) There are no background application processes that can be made active.
3) Apart from the above 2, there plenty that are limited
a) only 1 active window
b) access
c) system resources, around 64mb is what can be used by your active application
d) response time
e) screen size
Btw, the same rules apply to building an App for the IPOD and if you look more broadly, the same development principles without many of the limitations mentioned above apply to development on MAC applications as well. Iphone after all is hosting a mini robust version of the OS X.
If you can see the XCode Iphone project templates screen on your dev env, you're all set to write your first application in the next part.....
There may be many reasons why someone would want to be an app developer for the Iphone. Mobile platform, technology, money & fame, Innovative, etc. Even though the upfront cost is more and requires non-apple platform users/developers to do the switch, Apple created the App store in such a way that a good application can actually mean a lot of ROI (10s or even 100s of times more than what is spent for developing the same) and fame to the platform as well as the developer who built it. It is also the sure path to become innovative.
With this article, I am starting a tri-part series on how to setup the OS X environment and build your First Iphone application. Feel free to skip this part if you already know how to get the XCode interface up and running on OS X Leopard.
Who can do it
Anyone from any platform Windows, Unix, etc can do it. The platform switch sounds difficult & scary but not anymore after you see how easy it is to start OFF, it get to be a lot of fun.
Cost
The upfront cost to build an Iphone application, if you don't already own a Mac computer is around $900 while discounting the time spent learning the Objective C language constructs and writing the application code using XCode. Though Apple did an Amazing job to reduce the complexity of development by structuring the entire development & deployment process, it is still difficult for non-apple platform developers to get on the band wagon.
For those who are beginners -
The easiest way to start is by getting hold of a Mac (IMac, MacBook or any Mac hardware based computer) with OS X Leopard to setup the development environment for the application that we're going to build. I tried finding ways to setup a virtual machine, and most of the blogs out there say that it is not possible for 3 reasons:
1) Apple does not allow this in their OS licensing
2) Mac OS X specifically checks to ensure that it is installing on Apple hardware
3) Mac OS X requires that the computer has an APIC - which we do not emulate
Sometime last year Apple decided to open up their software platform to let the users/developers benefit from virtualizing OS X. But the catch is it can be done only on MAC hardware.
So, let's do the costing math
Mac Mini - $599.00
Mac OS X - $129.00
Apple Developer License - $99.00 (you need this at least at the end to publish an application in the app store)
You can always look for a Refurb Mini which can reduce the cost by another $100-$200. But even then the overall upfront cost to start is around $500 assuming you have a keyboard/mouse & a monitor for reuse from the computer you have at home.
I tried looking into this further to find out if there is any other way to do this cheap. i found one other way where you only need a licensed copy of OS X Leopard.
Note: Don't try this unless you're OK wasting time & the $129 if you realize that this may not support the development environment. The PEARPC Mac hardware emulator. Install instructions, refer to pearpc from sourceforge or http://pearpc.sourceforge.net/. This option would still cost you $129 assuming you have a windows based personal computer at home. (You're on your own if you're doing this.)
The DEV environment
Download & Install the free Iphone SDK by registering at Apple iphone developer portal. For now, you don't need to opt for the $99 DEV license as building application doesn't require it. To download, you'll need to register with apple and that is FREE.
However, The free download option will not let you download the applications you build on to your Iphone device for end to end performance testing. It'll also not let you submit the final application to be posted in the App store. But you'll not need this to learn and be comfortable with the platform switch. If you're so comfortable, there is always the Jailbroken mode to get your app into the Device the hard way...
Iphone SDK Contents
XCode - Apple's integrated development environment (IDE). This includes editor for creating, debugging source code, compilation and performance tuning tools.
Simulator - This will allow the developers to run their Iphone programs on Mac.
All set and before you begin
All the code you write must be in Objective C. This is very similar to C#, Java or any other objective programming language. I've heard/learnt somewhere that even JS can also be used to create an Iphone application. If you're not already familiar with Objective C, you may refer to the reference guide available thru the apple developer connection, http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/objectiveC.
The xcode Iphone project templates...

Also, make yourself familiar with the Iphone/Ipod interface because you wanted to build something for this platform.
Limitations
1) The platform currently allows only 1 actively running application other than the OS at any given time. (It is phone running on battery!)
2) There are no background application processes that can be made active.
3) Apart from the above 2, there plenty that are limited
a) only 1 active window
b) access
c) system resources, around 64mb is what can be used by your active application
d) response time
e) screen size
Btw, the same rules apply to building an App for the IPOD and if you look more broadly, the same development principles without many of the limitations mentioned above apply to development on MAC applications as well. Iphone after all is hosting a mini robust version of the OS X.
If you can see the XCode Iphone project templates screen on your dev env, you're all set to write your first application in the next part.....
Subscribe to:
Posts (Atom)