RubyMotion>>如何发布POST?

时间:2012-07-06 10:04:45

标签: rubymotion

我一直在查看RestKit和GET的工作

@client.get("/?amount=5", delegate:self)

有谁知道如何发布POST并收到结果?

文档提到它看起来像这样 -

@client.post("/ask", params:@paramvar, delegate:self)

你将@paramvar封装起来了什么?我已经尝试过它作为一个数组,哈希甚至是零 - 然而,它们都没有产生任何结果。

3 个答案:

答案 0 :(得分:6)

看看气泡包装库。它包括一些非常好的HTTP助手。

http://bubblewrap.io/

答案 1 :(得分:3)

在RubyMotion_Cookbook中找到一个例子。

https://github.com/IconoclastLabs/rubymotion_cookbook/tree/master/ch_8/06_sendinghttppost

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = RootController.new

    url_string = "http://rubymotion-cookbook.herokuapp.com/post"
    post_body = "bodyParam1=BodyValue1&bodyParam2=BodyValue2"

    url = NSURL.URLWithString(url_string)
    request = NSMutableURLRequest.requestWithURL(url)
    request.setTimeoutInterval(30)
    request.setHTTPMethod("POST")
    request.setHTTPBody(post_body.to_s.dataUsingEncoding(NSUTF8StringEncoding))
    queue = NSOperationQueue.alloc.init

    NSURLConnection.sendAsynchronousRequest(request,
      queue: queue,
      completionHandler: lambda do |response, data, error|
        if(data.length > 0 && error.nil?)
          html = NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
          p "HTML = #{html}"
        elsif( data.length == 0 && error.nil? )
          p "Nothing was downloaded"
        elsif(!error.nil?)
          p "Error: #{error}"
        end
      end
    )


    @window.makeKeyAndVisible
    true
  end
end

答案 2 :(得分:0)

来源: https://github.com/rubymotion/BubbleWrap

Insallation: -
在控制台中运行'gem install bubble-wrap'或在Gemfile中提及'gem bubble-wrap'

要添加到'app_delegate.rb'文件中的行(通过此Bubblewrap api可通过应用程序获得): -
需要'bubble-wrap / http'

语法示例代码: -
BW :: HTTP.get(“https://api.github.com/users/mattetti”,{凭证:{用户名:'matt',密码:'aimonetti'}})做|响应|   p response.body.to_str#打印响应的正文 端