incubator-flex-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dahn Maier <d...@maierinc.com>
Subject Re: flex and .net mvc4 webapi rest service
Date Wed, 21 Nov 2012 13:53:18 GMT
Use HTTPService and serialize / deserialize your as typed objects to / from
json


Here's a generic rest api call:

*// use a HTTPService - first configure it*

 *private* *function* configureService():*void* {

 service = *new* HTTPService();

// REST service path ie :"www.example.com/admin/test";

 service.url = Config.WEB_ROOT+*"/path/to/method"*;

 service.method = *"POST"*;

 service.useProxy = *false* ;

// set your response / fault event handlers

 service.addEventListener(ResultEvent.RESULT, onUrlSuccess) ;

 service.addEventListener(FaultEvent.FAULT, onUrlFault) ;

 }

// call the service with the json object
public function callService(myObject:MyType):void{
    var myObjectJson:String = myObject.toJson();
    var data:Object = new Object();
// send the json as a post property
    data.json = myObjectJson;
    service.send( data);
// or send the json directly (depends on what your service expects
    service.send(myObjectJson;
}

You also need to serialize your object as Json - I do it manually (very
simple entity) using the JSON class provided by Flex but you can also use
more sophisticated mechanisms if you have more complex objects (see
documentation on JSON or use as3corelib library)

Here is a class for sending image information back and forth with json
serialization / deserialization

 *public* *class* ImageInfo

{

 *public* *var* contentType:String ;

 *public* *var* fileName:String ;

 *public* *var* fileSize:int;

 *public* *var* imageRatio:Number;

 *public* *var* published:Boolean;

 *public* *var* sessionId:String;

 *public* *var* stringKey:String;

 *public* *var* title:String;

 *public* *var* uploadUrl:String;

 *public* *var* userKeyId:String;

 *public* *var* userName:String;

 *public* *var* searchTags:Array;

  /**

 * Constructor.

 */

 *public* *function* ImageInfo(jsonResponse:Object = *null*) {

 *if*(jsonResponse != *null*)

  fromJson(jsonResponse);

 }

 /**

 *

 */

 *public* *function* toJson():*{

 *// stringify the arrays separately*

   *var* searchTagsJson:Array = [];

 *if*(searchTags != *null* && searchTags.length > 0 ){

  *var* st:ArrayCollection = *new* ArrayCollection(searchTags);

  *for each* (*var* search:String *in* st){

  searchTagsJson.push(search);

  }

 }

   *var* json:Object = {

 *"contentType"*:*this*.contentType,

 *"fileName"*:*this*.fileName,

 *"fileSize"*:*this*.fileSize,

 *"imageRatio"*:*this*.imageRatio,

 *"published"*:*this*.published,

 *"sessionId"*:*this*.sessionId,

 *"stringKey"*:*this*.stringKey,

 *"title"*:*this*.title,

 *"uploadUrl"*:*this*.uploadUrl,

 *"userKeyId"*:*this*.userKeyId,

 *"userName"*:*this*.userName,

 *"space"*:spaceJson,

 *"searchTags"*:searchTagsJson};

   *return* JSON.stringify(json);

 }

  /**

 * Revives the object from a json object representation.

 */

 *private* *function* fromJson(json:Object):*void*{

 *var* thisXML:XML = describeType(*this*);

 *for* ( *var* a:Object *in* thisXML.variable ){

  *var* propertyName:String = thisXML.variable.@name[a];

    *if*(json.hasOwnProperty(propertyName)){

  *this*[*propertyName*] = json[propertyName];

  }

 }

 }

On Wed, Nov 21, 2012 at 4:03 AM, Franca Daniel <dffbr@yahoo.com.br> wrote:

> Hello,
> Any idea how to pass and receive objects to a  asp.net webapi
> rest service (Json format) ?
>
> Thank you!




-- 
Dahn Maier
Maier Inc.
dahn@maierinc.com
917.596.3685

------------------------------
This transmission is intended solely for the person or organization to whom
it is addressed and it may contain privileged and confidential information.
If you are not the intended recipient you should not copy, distribute or
take any action in reliance on it. If you believe you received this
transmission in error please notify the sender.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message