我想在我的 Flash As3 应用程序中使用来自 mashape 的 API
I would like to use an API from mashape in my flash As3 app
我想在 ActionScript 中加载 api。就是在这个linkhttps://market.mashape.com/vivekn/sentiment-3
curl 请求是这样的:
curl -X POST --include 'https://community-sentiment.p.mashape.com/text/' \
-H 'X-Mashape-Key: YourPrivateApiKeyHere' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' \
-d 'txt=Today is a good day'
我将如何在 AS3 中使用它。提前致谢。
您可以使用 URLLoader class 进行 REST 调用。
将 api 的自定义 headers 映射到 URLRequestHeader classes and the custom http options to a URLRequest class。
示例跟踪输出:
[trace] {
[trace] "result": {
[trace] "confidence": "72.7805",
[trace] "sentiment": "Negative"
[trace] }
[trace] }
[trace] 72.7805
[trace] Negative
ActionScript cut/paste 示例(只需添加您的私人事故 api 密钥):
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
public class Main extends Sprite {
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
var textField3:TextField = new TextField();
public function Main() {
textField1.text = "Today is a bad day";
addChild(textField1);
textField2.x = 150;
addChild(textField2);
textField3.x = 200;
addChild(textField3);
var apiKey:String = "your api key here";
var url:String = "https://community-sentiment.p.mashape.com/text/";
var headers:Array = [
new URLRequestHeader("X-Mashape-Key", apiKey),
new URLRequestHeader("Accept", "application/json"),
new URLRequestHeader("Content-Type","application/x-www-form-urlencoded")
];
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
request.requestHeaders = headers;
request.data = "txt=" + textField1.text;
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, httpRequestComplete, false, 0, true);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpRequestError, false, 0, true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, httpRequestError, false, 0, true);
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
urlLoader.load(request);
}
private function httpRequestComplete(event:Event):void {
trace(event.target.data);
var response:Object = JSON.parse(event.target.data);
trace(response.result.confidence);
trace(response.result.sentiment);
textField2.text = response.result.confidence;
textField3.text = response.result.sentiment;
}
private function httpRequestError(event:IOErrorEvent):void {
trace("Error: " + event.errorID + " " + event.text);
}
function httpStatusHandler(event:HTTPStatusEvent):void {
if (event.status != 200) {
trace("httpStatusHandler: " + event.status);
}
}
}
}
我不会对@RobertN 的回答添加太多,只是一些小的解释。
要使用 ActionScript 执行 HTTP 请求,您需要:
A URLRequest
object 将执行 HTTP 请求。
A URLLoader
object 将下载数据。
A URLVariables
object 用 HTTP 请求发送一些变量。
一些 URLRequestHeader
objects 发送一些自定义 HTTP 请求 headers.
// create some variables
var url_variables:URLVariables = new URLVariables();
url_variables.txt = 'hello';
// set the URL of the HTTP request
var url_request:URLRequest = new URLRequest('https://community-sentiment.p.mashape.com/text/')
// set the content type
url_request.contentType = 'application/x-www-form-urlencoded';
// set the request method
url_request.method = URLRequestMethod.POST;
// set some request headers
url_request.requestHeaders.push(new URLRequestHeader('X-Mashape-Key', 'your-key-here'));
url_request.requestHeaders.push(new URLRequestHeader('Accept', 'application/json'));
// set the variables
url_request.data = url_variables;
var url_loader:URLLoader = new URLLoader();
url_loader.addEventListener(Event.COMPLETE, on_Complete);
url_loader.load(url_request);
function on_Complete(e:Event): void
{
trace(e.target.data);
// gives :
// {
// "result": {
// "confidence": "50.0000",
// "sentiment": "Neutral"
// }
// }
}
有关 Web 服务请求的更多信息,请查看 here。
希望能帮到你。
我想在 ActionScript 中加载 api。就是在这个linkhttps://market.mashape.com/vivekn/sentiment-3 curl 请求是这样的:
curl -X POST --include 'https://community-sentiment.p.mashape.com/text/' \
-H 'X-Mashape-Key: YourPrivateApiKeyHere' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' \
-d 'txt=Today is a good day'
我将如何在 AS3 中使用它。提前致谢。
您可以使用 URLLoader class 进行 REST 调用。
将 api 的自定义 headers 映射到 URLRequestHeader classes and the custom http options to a URLRequest class。
示例跟踪输出:
[trace] {
[trace] "result": {
[trace] "confidence": "72.7805",
[trace] "sentiment": "Negative"
[trace] }
[trace] }
[trace] 72.7805
[trace] Negative
ActionScript cut/paste 示例(只需添加您的私人事故 api 密钥):
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
public class Main extends Sprite {
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
var textField3:TextField = new TextField();
public function Main() {
textField1.text = "Today is a bad day";
addChild(textField1);
textField2.x = 150;
addChild(textField2);
textField3.x = 200;
addChild(textField3);
var apiKey:String = "your api key here";
var url:String = "https://community-sentiment.p.mashape.com/text/";
var headers:Array = [
new URLRequestHeader("X-Mashape-Key", apiKey),
new URLRequestHeader("Accept", "application/json"),
new URLRequestHeader("Content-Type","application/x-www-form-urlencoded")
];
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
request.requestHeaders = headers;
request.data = "txt=" + textField1.text;
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, httpRequestComplete, false, 0, true);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpRequestError, false, 0, true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, httpRequestError, false, 0, true);
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
urlLoader.load(request);
}
private function httpRequestComplete(event:Event):void {
trace(event.target.data);
var response:Object = JSON.parse(event.target.data);
trace(response.result.confidence);
trace(response.result.sentiment);
textField2.text = response.result.confidence;
textField3.text = response.result.sentiment;
}
private function httpRequestError(event:IOErrorEvent):void {
trace("Error: " + event.errorID + " " + event.text);
}
function httpStatusHandler(event:HTTPStatusEvent):void {
if (event.status != 200) {
trace("httpStatusHandler: " + event.status);
}
}
}
}
我不会对@RobertN 的回答添加太多,只是一些小的解释。
要使用 ActionScript 执行 HTTP 请求,您需要:
A
URLRequest
object 将执行 HTTP 请求。A
URLLoader
object 将下载数据。A
URLVariables
object 用 HTTP 请求发送一些变量。一些
URLRequestHeader
objects 发送一些自定义 HTTP 请求 headers.
// create some variables var url_variables:URLVariables = new URLVariables(); url_variables.txt = 'hello'; // set the URL of the HTTP request var url_request:URLRequest = new URLRequest('https://community-sentiment.p.mashape.com/text/') // set the content type url_request.contentType = 'application/x-www-form-urlencoded'; // set the request method url_request.method = URLRequestMethod.POST; // set some request headers url_request.requestHeaders.push(new URLRequestHeader('X-Mashape-Key', 'your-key-here')); url_request.requestHeaders.push(new URLRequestHeader('Accept', 'application/json')); // set the variables url_request.data = url_variables; var url_loader:URLLoader = new URLLoader(); url_loader.addEventListener(Event.COMPLETE, on_Complete); url_loader.load(url_request); function on_Complete(e:Event): void { trace(e.target.data); // gives : // { // "result": { // "confidence": "50.0000", // "sentiment": "Neutral" // } // } }
有关 Web 服务请求的更多信息,请查看 here。
希望能帮到你。