|
ToDo:
|
もう半年が経ったんだー。前半はトラブル対応のみだったな。
合間を見つけてはコードをかいてみました。
ActionScriptでJSONをハンドリングするには、mikechambers/as3corelib · GitHubが便利です。
仕事ではPOSTを多用しそうです。GETが簡単ですけどね。POSTの時は配列になっているのはご愛敬です。:-P
package org.hikazoh
{
import com.adobe.serialization.json.*;
import com.adobe.serialization.json.JSON;
import flash.events.*;
import flash.net.*;
import flash.utils.*;
import mx.controls.Alert;
import mx.core.IMXMLObject;
import mx.events.FlexEvent;
import mx.events.Request;
import mx.utils.Base64Decoder;
import mx.utils.Base64Encoder;
public class RestfulJsonClient implements IMXMLObject
{
private var _doc:JsonServiceClient;
private var _requestor:URLLoader = new URLLoader();
public function RestfulJsonClient(){
}
public function initialized(doc:Object, id:String):void{
this._doc = doc as JsonServiceClient;
this._doc.addEventListener(FlexEvent.APPLICATION_COMPLETE,
loadCompleteHandler);
}
public function loadCompleteHandler(event:Event):void{
this._doc.Button01.addEventListener(MouseEvent.CLICK,
mouseClickHandler);
this._doc.Button02.addEventListener(MouseEvent.CLICK,
mousePostClickHandler);
}
public function mousePostClickHandler(event:MouseEvent):void{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, completePostHandler);
var request:URLRequest = new URLRequest("http://localhost:50814/RestfulService/PostSampleMethod");
request.method = "POST";
request.contentType = "application/json";
var param:Array = [{Name: this._doc.TextInput01.text}];
request.data = new JSONEncoder(param).getString();
try{
loader.load(request);
}
catch(e:Error){
Alert.show(e.message);
}
}
public function completePostHandler(event:Event):void{
try{
this._doc.Label01.text = new JSONDecoder(event.target.data as String,false).getValue().Message;
}
catch(e:Error){
Alert.show(e.message);
}
}
public function mouseClickHandler(event:MouseEvent):void{
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(new URLRequest("http://localhost:50814/RestfulService/GetSampleMethod/" + this._doc.TextInput01.text));
}
catch(e:Error){
trace(e.message);
}
}
public function completeHandler(event:Event):void{
try{
this._doc.Label01.text = event.target.data;
}
catch(e:Error){
trace(e.message);
}
}
}
}
.NETのほうがJavaより慣れているのです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Text;
using System.IO;
namespace RestJsonService
{
[ServiceContract]
public interface IRestfulService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetSampleMethod/{name}")]
string GetSampleMethod(string name);
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "PostSampleMethod")]
PostSampleResponse PostSampleMethod(Stream stream);
}
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
public class RestfulService : IRestfulService
{
public string GetSampleMethod(string name)
{
return "こんにちは" + name + "さん";
}
public PostSampleResponse PostSampleMethod(Stream stream)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PostSampleRequest[]));
PostSampleRequest [] req = (PostSampleRequest [])serializer.ReadObject(stream);
return new PostSampleResponse
{
Message = req[0].Name
};
}
}
[DataContract]
public class PostSampleRequest{
[DataMember(Name="Name")]
public string Name{ get; set;}
}
[DataContract]
public class PostSampleResponse{
[DataMember(Name="Message")]
public string Message {get; set;}
}
}
あとGlobal.asax.csで
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(
new ServiceRoute("RestfulService",
new WebServiceHostFactory(),
typeof(RestfulService))
);
}
必ず土曜日出勤で疲れた。
一作目が面白かったので観ました。まぁまぁかな。次回あるような感じです。ラストは原作にちょっと忠実だったかな。
かるーくランニングするつもりで外出。すっげー暑かった。6kmのうち2kmは歩いたかな。