Żądania usług Web Service typu RESTFlash Player 9 i nowsze wersje, Adobe AIR 1.0 i nowsze wersje Usługi Web Service typu REST wykorzystują komendy metod HTTP do określania podstawowej operacji i zmiennych URL w celu zdefiniowania szczegółów operacji. Na przykład, żądanie pobierania danych dla elementu może wykorzystywać komendę GET i zmienne URL w celu określenia nazwy metody i identyfikatora elementu. W rezultacie można uzyskać następujący ciąg URL: http://service.example.com/?method=getItem&id=d3452 Aby uzyskać dostęp do usługi Web Service typu REST za pomocą modułu ActionScript, można użyć klas URLRequest, URLVariables oraz URLLoader. W kodzie Javascript w obrębie aplikacji AIR można również wykorzystać obiekt XMLHttpRequest. Programowanie wywołania usługi Web Service typu REST w module ActionScript zazwyczaj wymaga przeprowadzenia następującej procedury:
Przykładem może być usługa Web Service udostępniająca metodę testowania, która odsyła parametry wywołania z powrotem do żądającego. Następujący kod ActionScipt mógłby zostać wykorzystany do wywołania usługi: import flash.events.Event;
import flash.events.ErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
private var requestor:URLLoader = new URLLoader();
public function restServiceCall():void
{
//Create the HTTP request object
var request:URLRequest = new URLRequest( "http://service.example.com/" );
request.method = URLRequestMethod.GET;
//Add the URL variables
var variables:URLVariables = new URLVariables();
variables.method = "test.echo";
variables.api_key = "123456ABC";
variables.message = "Able was I, ere I saw Elba.";
request.data = variables;
//Initiate the transaction
requestor = new URLLoader();
requestor.addEventListener( Event.COMPLETE, httpRequestComplete );
requestor.addEventListener( IOErrorEvent.IOERROR, httpRequestError );
requestor.addEventListener( SecurityErrorEvent.SECURITY_ERROR, httpRequestError );
requestor.load( request );
}
private function httpRequestComplete( event:Event ):void
{
trace( event.target.data );
}
private function httpRequestError( error:ErrorEvent ):void{
trace( "An error occured: " + error.message );
}
W języku Javascript w obrębie aplikacji AIR można wykonać to samo żądanie za pomocą obiektu XMLHttpRequest: <html>
<head><title>RESTful web service request</title>
<script type="text/javascript">
function makeRequest()
{
var requestDisplay = document.getElementById( "request" );
var resultDisplay = document.getElementById( "result" );
//Create a conveninece object to hold the call properties
var request = {};
request.URL = "http://service.example.com/";
request.method = "test.echo";
request.HTTPmethod = "GET";
request.parameters = {};
request.parameters.api_key = "ABCDEF123";
request.parameters.message = "Able was I ere I saw Elba.";
var requestURL = makeURL( request );
xmlhttp = new XMLHttpRequest();
xmlhttp.open( request.HTTPmethod, requestURL, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
resultDisplay.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
requestDisplay.innerHTML = requestURL;
}
//Convert the request object into a properly formatted URL
function makeURL( request )
{
var url = request.URL + "?method=" + escape( request.method );
for( var property in request.parameters )
{
url += "&" + property + "=" + escape( request.parameters[property] );
}
return url;
}
</script>
</head>
<body onload="makeRequest()">
<h1>Request:</h1>
<div id="request"></div>
<h1>Result:</h1>
<div id="result"></div>
</body>
</html>
|
|