gpt4 book ai didi

ruby - ActionScript 3 Json 请求

转载 作者:太空宇宙 更新时间:2023-11-03 16:52:45 26 4
gpt4 key购买 nike

我正在尝试使用 as3 将图像发送到服务器。我需要返回托管图像的 url,以便将其分享到 facebook 和 twitter。有人告诉我用 ruby 代码复制我在这里的东西,但我做不到。这是我的。

ruby 代码:

 require 'rest_client' # gem: rest-client
require 'json'

mcz_url = 'server URL'

mcz_response = nil

File.open(ARGV[0], 'r') do |f|
params = { :file => f, :type => 'image', :sizes => nil }

RestClient.post(mcz_url, params) do |response, request, result|
mcz_response = response
end
end

unless mcz_response.nil?
json = JSON.parse(mcz_response)
puts "CDN Url: #{json['original']}"
else
puts 'MCZ response is null'
end

ActionScript 3

  share_pic.addEventListener(MouseEvent.CLICK, publish); //buton to share a pic

function publish(evt:MouseEvent):void {
var jpgSource:BitmapData = new BitmapData (stage.width, 256);
jpgSource.draw(stage);
var jpgEncoder:JPGEncoder = new JPGEncoder(90);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource); //image
trace("sharing pic...");
var request = new URLRequest("Server URL");

request.method = URLRequestMethod.POST;
var messages:Array = new Array ();
messages.push ({field_name:"file:", value: jpgSource});
messages.push ({field_name:"type", value:"'image'"});
messages.push ({field_name:"sizes", value:"nil"});

var vars: URLVariables = new URLVariables();
vars.data = JSON.stringify(messages);
request.data = vars;
trace("vars= "+vars);
trace ("request= "+request);
trace("done");


var linkGenerated:String = "";//image string i would like to get from the JSON
var linkFace:String = "http://www.facebook.com/sharer.php?u="
var linkShare:String = linkFace + linkGenerated ;
var requestFace = new URLRequest(linkShare);
navigateToURL(requestFace,"_blank");

非常感谢您的帮助。

最佳答案

您可能想要使用 URLLoader.load() ,并且可能需要注意 FileReference.upload() 以备将来需要。在这两种情况下,您都需要使用带有处理程序的事件监听器拆分您的发布功能,一旦服务器完成其进程,这些处理程序将对结果进行操作...

public function publish():void
{
var urlToPost:URLRequest = new URLRequest("some_url_here");
//attach any post data, etc.
var urlLoader:URLLoader = new URLLoader();
//attach your event listeners, which will be called at some point in the future (asynchronous)
urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
//call load to start the upload
urlLoader.load( urlToPost );
}

protected function onLoadComplete(e:Event):void
{
var urlLoader:URLLoader = URLLoader(event.target);
var linkGenerated:String = urlLoader.data.some_image_string;//image string i would like to get from the JSON
var linkFace:String = "http://www.facebook.com/sharer.php?u="
var linkShare:String = linkFace + linkGenerated ;
var requestFace = new URLRequest(linkShare);
navigateToURL(requestFace,"_blank");
}

与您正在寻找的内容类似的问题/答案可以是 Found Here .

使用 FileReference.upload() 时,请务必为 DataEvent.UPLOAD_COMPLETE_DATA 附加一个事件监听器,以便您可以在上传完成后访问从服务器返回的任何数据。

关于ruby - ActionScript 3 Json 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17284162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com