Cappuccino Javascript Framework – Part 2

Consuming .NET JSON WebService – Part 2

The Cappuccino Code

On this post I’ll show you HOW-TO develop and consuming JSON Web Services using the Cappuccino Javascript Framework and .Net 2.0 Framework.

On this step let’s build the Cappuccino RIA Application that will consume the .Net Web Service.

Notes: About the “.d”.

Ok, let’s start writing the Cappuccino code:

//  AppController.h
//  Created by «FULLUSERNAME» on «DATE».
//  Copyright «ORGANIZATIONNAME» «YEAR». All rights reserved.

@import <Foundation/CPObject.j>
@import <AppKit/CPCib.j>

@import <Foundation/CPURLRequest.j>
@import <Foundation/CPJSONPConnection.j>

@import <AppKit/CPToolbar.j>
@import <AppKit/CPToolbarItem.j>
@import <AppKit/CPCollectionView.j>

@implementation AppController : CPObject
	// Main Window
    CPWindow			theWindow;

	 * URLConnection - Used to consume WebServices 
    CPURLConnection		urlConnection;

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
    var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
        contentView = [theWindow contentView];

    var label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];

    [label setStringValue:@"Hello World!"];
    [label setFont:[CPFont boldSystemFontOfSize:24.0]];

    [label sizeToFit];

    [label setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
    [label setFrameOrigin:CGPointMake((CGRectGetWidth([contentView bounds]) - CGRectGetWidth([label frame])) / 2.0, (CGRectGetHeight([contentView bounds]) - CGRectGetHeight([label frame])) / 2.0)];

    [contentView addSubview:label];

    [theWindow orderFront:self];

    /// Uncomment the following line to turn on the standard menu bar.
    //[CPMenu setMenuBarVisible:YES];
	 * Using the CPURLConnection to consume JSON .NET WebServices
    var request = [CPURLRequest requestWithURL:"http://localhost:8081/Services/XmlJson.asmx/ListJSON"];
	[request setHTTPMethod: "POST"]; 
    [request setValue:"application/json; charset=utf-8" forHTTPHeaderField:"Content-Type"]; 
	urlConnection = [CPURLConnection connectionWithRequest:request delegate:self];


 * CPURLConnection Delegate Methods

 * this method is the responsavel to treat the received data, is on this method that you have to manipulate your data.
- (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
	/// Convert the recived JSON data to Cappuccino Object
	var _data = CPJSObjectCreateWithJSON(data);

	/// the ".d" in .Net 2.0 or 3.5 JSON WebServices is implemented because of security measures.
	_data = eval(_data.d);
	alert("data = " + _data);
	alert("_data[0].firstName = " + _data[0].firstName);


 * If you have any type of error during the connection process, this is the method responsible to notify you about the error.
- (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)error
    [self connectionDidFinishLoading:aConnection];

 * this is the method responsavel to tell you that the process of receiving data was ended, 
 * and here you can release all unnecessary variables.
- (void)connectionDidFinishLoading:(CPURLConnection)aConnection
    if (aConnection == urlConnection)
        urlConnection = nil;


Download the source code, with Cappuccino Framework.

Consuming .Net Web Services with Cappuccino.

2 Replies to “Cappuccino Javascript Framework – Part 2”

  1. whoah this blog is fantastic i love reading your posts. Keep up the great work! You know, lots of people are looking around for this information, you could aid them greatly.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.