//
// AppController.h
// «PROJECTNAME»
//
// 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
{
alert(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;
}
@end
Download the source code, with Cappuccino Framework.
[wpdm_file id=10]
On this post I’ll show you HOW-TO develop and consuming JSON Web Services using the Cappuccino Javascript Framework and .Net 2.0 / 3.5 Framework.
In first step, let’s build the JSON .Net WebService, using Microsoft or Mono Framework. To simplify this post I’ll not use the code behind.
Notes:
As we know, to use JSON on .NET WebServices we need to use the following assembly “Microsoft.Web.Extensions.dll”, and in the code we need to define the following namespaces: System.Web.Script.Services, System.Web.Script.Serialization.
Ok, let’s start writing the .Net JSON WebService (XmlJson.asmx) code:
<%@ WebService Language="C#" Class="Services.XmlJson" %>
using System;
using System.Globalization;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
// Script namespace from Microsoft.Web.Extensions.dll assembly
using System.Web.Script.Services;
using System.Web.Script.Serialization;
namespace Services
{
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX.
[System.Web.Script.Services.ScriptService]
public class XmlJson : System.Web.Services.WebService
{
private Contact _Contact;
public XmlJson() { }
[WebMethod]
public string About()
{
return "Version 1.0";
}
[WebMethod]
// ResponseFormat is JSON
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string ListJSON()
{
ArrayList list = new ArrayList();
_Contact = new Contact("First Name 1", "Last Name 1");
list.Add(_Contact);
_Contact = new Contact("First Name 2", "Last Name 2");
list.Add(_Contact);
Contact[] resultList = (Contact[]) list.ToArray(typeof(Contact));
// JSON Response
JavaScriptSerializer jss = new JavaScriptSerializer();
string serializedObj = jss.Serialize(resultList);
return serializedObj;
}
}
///
/// our serializable contact class
///
[System.Xml.Serialization.SoapType(Namespace="urn:XmlJsonContacts")]
public class Contact
{
private string _firstName;
private string _lastName;
public Contact() {}
public Contact(string firstName, string lastName){
this._firstName = firstName;
this._lastName = lastName;
}
public string firstName
{
get { return this._firstName;}
set { this._firstName = value;}
}
public string lastName
{
get { return this._lastName;}
set { this._lastName = value;}
}
}
}
Download the .Net source code.
[wpdm_file id=9]
Running ASPX Web Services on Mac OS X using Mono.
Create a Mono Develop Solution for the source code.