动作脚本3 Json请求

时间:2013-06-24 20:07:54

标签: ruby json actionscript-3 request

我正在尝试使用as3将图像发送到服务器。 我需要托管图片的网址,以便我可以分享到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

动作脚本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");

}

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可能希望使用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附加事件侦听器,以便在上载完成后可以从服务器访问任何返回的数据。